Skip to content

Commit

Permalink
add log
Browse files Browse the repository at this point in the history
  • Loading branch information
deardeng committed May 13, 2024
1 parent 4d82d13 commit a41751f
Showing 1 changed file with 57 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.doris.common.PatternMatcherException;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
import org.apache.doris.common.util.QueryableReentrantReadWriteLock;
import org.apache.doris.mysql.MysqlPassword;
import org.apache.doris.persist.gson.GsonPostProcessable;
import org.apache.doris.persist.gson.GsonUtils;
Expand All @@ -54,22 +55,21 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class UserManager implements Writable, GsonPostProcessable {
public static final String ANY_HOST = "%";
private static final Logger LOG = LogManager.getLogger(UserManager.class);

private static final ReadWriteLock rwlock = new ReentrantReadWriteLock();
private static final Lock rlock = rwlock.readLock();
private static final Lock wlock = rwlock.writeLock();
private static final QueryableReentrantReadWriteLock rwLock = new QueryableReentrantReadWriteLock(false);
private static final Lock rlock = rwLock.readLock();
private static final Lock wlock = rwLock.writeLock();

// One name may have multiple User,because host can be different
@SerializedName(value = "nameToUsers")
private Map<String, List<User>> nameToUsers = Maps.newConcurrentMap();

public boolean userIdentityExist(UserIdentity userIdentity, boolean includeByDomain) {
LOG.info("dx test before rlock userIdentityExist {}", rwLock.getOwner());
rlock.lock();
try {
List<User> users = nameToUsers.get(userIdentity.getQualifiedUser());
Expand All @@ -86,16 +86,19 @@ public boolean userIdentityExist(UserIdentity userIdentity, boolean includeByDom
return false;
} finally {
rlock.unlock();
LOG.info("dx test after rlock userIdentityExist");
}
}

public List<User> getUserByName(String name) {
LOG.info("dx test before rlock getUserByName {}", rwLock.getOwner());
rlock.lock();
try {
List<User> users = nameToUsers.get(name);
return users == null ? Collections.EMPTY_LIST : users;
} finally {
rlock.unlock();
LOG.info("dx test after rlock getUserByName {}", rwLock.getOwner());
}
}

Expand All @@ -113,6 +116,7 @@ private void checkPasswordInternal(String remoteUser, String remoteHost, byte[]
String remotePasswdStr, List<UserIdentity> currentUser, boolean plain) throws AuthenticationException {
PasswordPolicyManager passwdPolicyMgr = Env.getCurrentEnv().getAuth().getPasswdPolicyManager();
List<User> users = new ArrayList<>();
LOG.info("dx test before rlock checkPasswordInternal {}", rwLock.getOwner());
rlock.lock();
try {
users = nameToUsers.get(remoteUser);
Expand All @@ -122,6 +126,7 @@ private void checkPasswordInternal(String remoteUser, String remoteHost, byte[]
}
} finally {
rlock.unlock();
LOG.info("dx test after rlock checkPasswordInternal {}", rwLock.getOwner());
}

for (User user : users) {
Expand Down Expand Up @@ -157,6 +162,7 @@ private void checkPasswordInternal(String remoteUser, String remoteHost, byte[]

public List<UserIdentity> getUserIdentityUncheckPasswd(String remoteUser, String remoteHost) {
List<UserIdentity> userIdentities = Lists.newArrayList();
LOG.info("dx test before rlock getUserIdentityUncheckPasswd {}", rwLock.getOwner());
rlock.lock();
try {
List<User> users = nameToUsers.getOrDefault(remoteUser, Lists.newArrayList());
Expand All @@ -169,6 +175,7 @@ public List<UserIdentity> getUserIdentityUncheckPasswd(String remoteUser, String
return userIdentities;
} finally {
rlock.unlock();
LOG.info("dx test after rlock getUserIdentityUncheckPasswd {}", rwLock.getOwner());
}
}

Expand All @@ -195,6 +202,7 @@ private boolean comparePassword(Password curUserPassword, byte[] remotePasswd,


public void clearEntriesSetByResolver() {
LOG.info("dx test before wlock clearEntriesSetByResolver {}", rwLock.getOwner());
wlock.lock();
try {
Iterator<Entry<String, List<User>>> iterator = nameToUsers.entrySet().iterator();
Expand All @@ -215,11 +223,24 @@ public void clearEntriesSetByResolver() {
}
} finally {
wlock.unlock();
LOG.info("dx test after wlock clearEntriesSetByResolver {}", rwLock.getOwner());
}
}

public User createUser(UserIdentity userIdent, byte[] pwd, UserIdentity domainUserIdent, boolean setByResolver,
String comment)
String comment) throws PatternMatcherException {
LOG.info("dx test before wlock createUser {}", rwLock.getOwner());
wlock.lock();
try {
return createUserWithoutLock(userIdent, pwd, domainUserIdent, setByResolver, comment);
} finally {
wlock.unlock();
LOG.info("dx test after wlock createUser {}", rwLock.getOwner());
}
}

public User createUserWithoutLock(UserIdentity userIdent, byte[] pwd, UserIdentity domainUserIdent,
boolean setByResolver, String comment)
throws PatternMatcherException {
if (userIdentityExist(userIdent, true)) {
User userByUserIdentity = getUserByUserIdentity(userIdent);
Expand All @@ -238,23 +259,20 @@ public User createUser(UserIdentity userIdent, byte[] pwd, UserIdentity domainUs
PatternMatcher hostPattern = PatternMatcher
.createMysqlPattern(userIdent.getHost(), CaseSensibility.HOST.getCaseSensibility());
User user = new User(userIdent, pwd, setByResolver, domainUserIdent, hostPattern, comment);
wlock.lock();
try {
List<User> nameToLists = nameToUsers.get(userIdent.getQualifiedUser());
if (CollectionUtils.isEmpty(nameToLists)) {
nameToLists = Lists.newArrayList(user);
nameToUsers.put(userIdent.getQualifiedUser(), nameToLists);
} else {
nameToLists.add(user);
Collections.sort(nameToLists);
}
return user;
} finally {
wlock.unlock();
List<User> nameToLists = nameToUsers.get(userIdent.getQualifiedUser());
if (CollectionUtils.isEmpty(nameToLists)) {
nameToLists = Lists.newArrayList(user);
nameToUsers.put(userIdent.getQualifiedUser(), nameToLists);
} else {
nameToLists.add(user);
Collections.sort(nameToLists);
}
return user;

}

public User getUserByUserIdentity(UserIdentity userIdent) {
LOG.info("dx test before wlock getUserByUserIdentity {}", rwLock.getOwner());
rlock.lock();
try {
List<User> nameToLists = nameToUsers.get(userIdent.getQualifiedUser());
Expand All @@ -271,10 +289,12 @@ public User getUserByUserIdentity(UserIdentity userIdent) {
return null;
} finally {
rlock.lock();
LOG.info("dx test after wlock getUserByUserIdentity {}", rwLock.getOwner());
}
}

public void removeUser(UserIdentity userIdent) {
LOG.info("dx test before wlock removeUser {}", rwLock.getOwner());
wlock.lock();
try {
List<User> nameToLists = nameToUsers.get(userIdent.getQualifiedUser());
Expand All @@ -295,15 +315,18 @@ public void removeUser(UserIdentity userIdent) {
}
} finally {
wlock.unlock();
LOG.info("dx test after wlock removeUser {}", rwLock.getOwner());
}
}

public Map<String, List<User>> getNameToUsers() {
LOG.info("dx test before rlock getNameToUsers {}", rwLock.getOwner());
rlock.lock();
try {
return ImmutableMap.copyOf(nameToUsers);
} finally {
rlock.unlock();
LOG.info("dx test after rlock getNameToUsers {}", rwLock.getOwner());
}
}

Expand All @@ -319,6 +342,7 @@ public void setPassword(UserIdentity userIdentity, byte[] password, boolean errO
}

public void getAllDomains(Set<String> allDomains) {
LOG.info("dx test before rlock getAllDomains {}", rwLock.getOwner());
rlock.lock();
try {
for (Entry<String, List<User>> entry : nameToUsers.entrySet()) {
Expand All @@ -329,6 +353,7 @@ public void getAllDomains(Set<String> allDomains) {
}
}
} finally {
LOG.info("dx test before rlock getAllDomains {}", rwLock.getOwner());
rlock.unlock();
}
}
Expand All @@ -337,7 +362,8 @@ public void getAllDomains(Set<String> allDomains) {
// it will only modify password entry of these resolved IPs. All other privileges are binded
// to the domain, so no need to modify.
public void addUserPrivEntriesByResolvedIPs(Map<String, Set<String>> resolvedIPsMap) {
rlock.lock();
LOG.info("dx test before wlock addUserPrivEntriesByResolvedIPs {}", rwLock.getOwner());
wlock.lock();
try {
for (Entry<String, List<User>> userEntry : nameToUsers.entrySet()) {
for (Map.Entry<String, Set<String>> entry : resolvedIPsMap.entrySet()) {
Expand All @@ -352,15 +378,16 @@ public void addUserPrivEntriesByResolvedIPs(Map<String, Set<String>> resolvedIPs
byte[] password = domainUser.getPassword().getPassword();
Preconditions.checkNotNull(password, entry.getKey());
try {
createUser(userIdent, password, domainUser.getUserIdentity(), true, "");
createUserWithoutLock(userIdent, password, domainUser.getUserIdentity(), true, "");
} catch (PatternMatcherException e) {
LOG.info("failed to create user for user ident: {}, {}", userIdent, e.getMessage());
}
}
}
}
} finally {
rlock.unlock();
wlock.unlock();
LOG.info("dx test before wlock addUserPrivEntriesByResolvedIPs {}", rwLock.getOwner());
}
}

Expand All @@ -375,11 +402,13 @@ private User getDomainUser(List<User> users, String domain) {

@Override
public String toString() {
LOG.info("dx test before rlock toString {}", rwLock.getOwner());
rlock.lock();
try {
return nameToUsers.toString();
} finally {
rlock.unlock();
LOG.info("dx test before rlock toString {}", rwLock.getOwner());
}
}

Expand All @@ -397,6 +426,7 @@ public static UserManager read(DataInput in) throws IOException {
// should be removed after version 3.0
private void removeClusterPrefix() {
Map<String, List<User>> newNameToUsers = Maps.newHashMap();
LOG.info("dx test before wlock removeClusterPrefix {}", rwLock.getOwner());
wlock.lock();
try {
for (Entry<String, List<User>> entry : nameToUsers.entrySet()) {
Expand All @@ -406,6 +436,7 @@ private void removeClusterPrefix() {
this.nameToUsers = newNameToUsers;
} finally {
wlock.unlock();
LOG.info("dx test after wlock removeClusterPrefix {}", rwLock.getOwner());
}
}

Expand All @@ -416,15 +447,18 @@ public void gsonPostProcess() throws IOException {

// ====== CLOUD ======
public Set<String> getAllUsers() {
LOG.info("dx test before rlock getAllUsers {}", rwLock.getOwner());
rlock.lock();
try {
return new HashSet<>(nameToUsers.keySet());
} finally {
rlock.unlock();
LOG.info("dx test before rlock getAllUsers {}", rwLock.getOwner());
}
}

public String getUserId(String userName) {
LOG.info("dx test before rlock getUserId {}", rwLock.getOwner());
rlock.lock();
try {
if (!nameToUsers.containsKey(userName)) {
Expand All @@ -441,6 +475,7 @@ public String getUserId(String userName) {
return userId;
} finally {
rlock.unlock();
LOG.info("dx test after rlock getUserId {}", rwLock.getOwner());
}
}

Expand Down

0 comments on commit a41751f

Please sign in to comment.