Skip to content

Commit ddc26c6

Browse files
author
shiwenyan
committed
userid-v1
1 parent 9f699e1 commit ddc26c6

File tree

7 files changed

+141
-1
lines changed

7 files changed

+141
-1
lines changed

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/entity/IEntityAccessor.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ public interface IEntityAccessor extends SnapshotProcessor {
3535
*/
3636
Role loadEntity(String entityName) throws IOException;
3737

38+
/**
39+
* Deserialize userid from lower storage.
40+
*
41+
* @return The max userid.
42+
* @throws IOException if an exception is raised when interacting with the lower storage.
43+
*/
44+
long loadUserId() throws IOException;
45+
46+
/**
47+
* save maxUserId to lower storage when snapshot.
48+
*
49+
* @throws IOException if an exception is raised when interacting with the lower storage.
50+
*/
51+
void saveUserId(long nextUserId) throws IOException;
52+
3853
/**
3954
* Serialize the entity object to lower storage.
4055
*

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/entity/User.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
/** This class contains all information of a User. */
3535
public class User extends Role {
3636

37+
private long userId;
38+
3739
private String password;
3840

3941
private Set<String> roleSet;
@@ -56,6 +58,13 @@ public User(String name, String password) {
5658
this.roleSet = new HashSet<>();
5759
}
5860

61+
public User(String name, String password, long userId) {
62+
super(name);
63+
this.password = password;
64+
this.userId = userId;
65+
this.roleSet = new HashSet<>();
66+
}
67+
5968
/** ---------- set func ---------------* */
6069
public void setPassword(String password) {
6170
this.password = password;
@@ -73,7 +82,15 @@ public void addRole(String roleName) {
7382
roleSet.add(roleName);
7483
}
7584

85+
public void setUserId(long userId) {
86+
this.userId = userId;
87+
}
88+
7689
/** ------------ get func ----------------* */
90+
public long getUserId() {
91+
return userId;
92+
}
93+
7794
public String getPassword() {
7895
return password;
7996
}

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ public Role loadEntity(String entityName) throws IOException {
223223
}
224224
}
225225

226+
@Override
227+
public long loadUserId() throws IOException {
228+
File userIdFile = checkFileAvailable("user_id", "");
229+
if (userIdFile == null) {
230+
return -1;
231+
}
232+
FileInputStream inputStream = new FileInputStream(userIdFile);
233+
try (DataInputStream dataInputStream =
234+
new DataInputStream(new BufferedInputStream(inputStream))) {
235+
return dataInputStream.readLong();
236+
} catch (Exception e) {
237+
throw new IOException(e);
238+
} finally {
239+
strBufferLocal.remove();
240+
}
241+
}
242+
226243
@Override
227244
public void saveEntity(Role entity) throws IOException {
228245
File roleProfile =
@@ -378,4 +395,35 @@ public void cleanEntityFolder() {
378395
LOGGER.warn("Role folder not exists");
379396
}
380397
}
398+
399+
@Override
400+
public void saveUserId(long nextUserId) throws IOException {
401+
File userInfoProfile =
402+
SystemFileFactory.INSTANCE.getFile(
403+
entityDirPath
404+
+ File.separator
405+
+ "user_id"
406+
+ IoTDBConstant.PROFILE_SUFFIX
407+
+ TEMP_SUFFIX);
408+
File userDir = new File(entityDirPath);
409+
if (!userDir.exists() && !userDir.mkdirs()) {
410+
LOGGER.error("Failed to create user dir {}", entityDirPath);
411+
}
412+
413+
try (FileOutputStream fileOutputStream = new FileOutputStream(userInfoProfile);
414+
BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream)) {
415+
IOUtils.writeLong(outputStream, nextUserId, encodingBufferLocal);
416+
outputStream.flush();
417+
fileOutputStream.getFD().sync();
418+
} catch (Exception e) {
419+
LOGGER.warn("meet error when save userId: {}", nextUserId);
420+
throw new IOException(e);
421+
} finally {
422+
encodingBufferLocal.remove();
423+
}
424+
File oldFile =
425+
SystemFileFactory.INSTANCE.getFile(
426+
entityDirPath + File.separator + "user_id" + IoTDBConstant.PROFILE_SUFFIX);
427+
IOUtils.replaceFile(userInfoProfile, oldFile);
428+
}
381429
}

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/BasicUserManager.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.slf4j.Logger;
3636
import org.slf4j.LoggerFactory;
3737

38+
import java.io.IOException;
39+
3840
/** This class stores information of each user. */
3941
public abstract class BasicUserManager extends BasicRoleManager {
4042

@@ -50,6 +52,8 @@ protected String getNoSuchEntityError() {
5052
return "No such user %s";
5153
}
5254

55+
protected long nextUserId = 9999;
56+
5357
/**
5458
* BasicUserManager Constructor.
5559
*
@@ -102,6 +106,20 @@ private void initAdmin() throws AuthException {
102106
LOGGER.info("Admin initialized");
103107
}
104108

109+
private void initUserId() {
110+
try {
111+
long maxUserId = this.accessor.loadUserId();
112+
if (maxUserId == -1 || maxUserId < 10000) {
113+
nextUserId = 10000;
114+
} else {
115+
nextUserId = maxUserId;
116+
}
117+
} catch (IOException e) {
118+
LOGGER.warn("meet error in load max userId.");
119+
throw new RuntimeException(e);
120+
}
121+
}
122+
105123
@Override
106124
public User getEntity(String entityName) {
107125
return (User) super.getEntity(entityName);
@@ -128,7 +146,15 @@ public boolean createUser(
128146
}
129147
lock.writeLock(username);
130148
try {
131-
user = new User(username, enableEncrypt ? AuthUtils.encryptPassword(password) : password);
149+
long userid = 0;
150+
if (username.equals("root")) {
151+
userid = 0;
152+
} else {
153+
userid = ++nextUserId;
154+
}
155+
user =
156+
new User(
157+
username, enableEncrypt ? AuthUtils.encryptPassword(password) : password, userid);
132158
entityMap.put(username, user);
133159
return true;
134160
} finally {
@@ -197,6 +223,7 @@ private void init() throws AuthException {
197223
@Override
198224
public void reset() throws AuthException {
199225
super.reset();
226+
initUserId();
200227
initAdmin();
201228
}
202229

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ protected String getEntitySnapshotFileName() {
8989

9090
@Override
9191
protected void saveEntityName(BufferedOutputStream outputStream, Role role) throws IOException {
92+
IOUtils.writeLong(outputStream, ((User) role).getUserId(), encodingBufferLocal);
9293
super.saveEntityName(outputStream, role);
9394
IOUtils.writeString(
9495
outputStream, ((User) role).getPassword(), STRING_ENCODING, encodingBufferLocal);
@@ -165,6 +166,7 @@ public User loadEntity(String entityName) throws IOException {
165166
user.setPrivilegeList(pathPrivilegeList);
166167
} else {
167168
assert (tag == VERSION);
169+
user.setUserId(dataInputStream.readLong());
168170
user.setName(IOUtils.readString(dataInputStream, STRING_ENCODING, strBufferLocal));
169171
user.setPassword(IOUtils.readString(dataInputStream, STRING_ENCODING, strBufferLocal));
170172
loadPrivileges(dataInputStream, user);

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public boolean processTakeSnapshot(File snapshotDir) throws TException, IOExcept
3939
for (Map.Entry<String, Role> entry : entityMap.entrySet()) {
4040
accessor.saveEntity(entry.getValue());
4141
}
42+
accessor.saveUserId(nextUserId);
4243
return accessor.processTakeSnapshot(snapshotDir);
4344
}
4445

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/IOUtils.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,36 @@ public static void writeInt(
9292
outputStream.write(encodingBuffer.array(), 0, Integer.BYTES);
9393
}
9494

95+
/**
96+
* Write a long (8-byte) into the given stream.
97+
*
98+
* @param outputStream the destination to insert.
99+
* @param i the long value to be written.
100+
* @param encodingBufferLocal a ThreadLocal buffer may be passed to avoid frequent memory
101+
* allocations. A null may also be passed to use a local buffer.
102+
* @throws IOException when an exception raised during operating the stream.
103+
*/
104+
public static void writeLong(
105+
OutputStream outputStream, long i, ThreadLocal<ByteBuffer> encodingBufferLocal)
106+
throws IOException {
107+
108+
ByteBuffer encodingBuffer;
109+
if (encodingBufferLocal != null) {
110+
encodingBuffer = encodingBufferLocal.get();
111+
if (encodingBuffer == null) {
112+
// 8 bytes is exactly what we need for a long
113+
encodingBuffer = ByteBuffer.allocate(8);
114+
encodingBufferLocal.set(encodingBuffer);
115+
}
116+
} else {
117+
encodingBuffer = ByteBuffer.allocate(8);
118+
}
119+
120+
encodingBuffer.clear();
121+
encodingBuffer.putLong(i);
122+
outputStream.write(encodingBuffer.array(), 0, Long.BYTES);
123+
}
124+
95125
/**
96126
* Read a string from the given stream.
97127
*

0 commit comments

Comments
 (0)