Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

How can I define timeout in session lock? Can the session lock cross datacenter? #372

Open
denglifeng3 opened this issue Feb 22, 2019 · 0 comments

Comments

@denglifeng3
Copy link

denglifeng3 commented Feb 22, 2019

I try to create a new session like this: ImmutableSession.builder()
.name(sessionName).ttl("10s").behavior("delete").build()
But the session is not deleted after 10s.
Here is my all code:

@OverRide
public boolean lock(boolean block) {
final boolean[] flag = {true};
List dcList = CONSUL_CLIENT.catalogClient().getDatacenters();
out:
while (true) {
dc:
for (String dc : dcList) {
String sessionIdTemp = createSession(dc);
sessionIdMap.put(dc, sessionIdTemp);
flag[0] = flag[0] && CONSUL_CLIENT.keyValueClient().acquireLock(this.lockKey, sessionIdTemp);
if (!flag[0]) {
logger.warn("try to add request lock in dc:{} failed.", dc);
if (block && times.get() > 0) {
times.decrementAndGet();
continue out;
} else {
return false;
}
} else {
logger.info("try to add request lock in dc:{} success.", dc);
}
}
return flag[0];
}
}

private String createSession(String dc) {
    if (CONSUL_CLIENT.sessionClient().getSessionInfo(sessionName, dc).isPresent()) {
        return CONSUL_CLIENT.sessionClient().getSessionInfo(sessionName, dc).get().getId();
    } else {
        final ImmutableSession session = ImmutableSession.builder()
                .name(sessionName).ttl("10s").behavior("delete").build();
        SessionCreatedResponse session1 = CONSUL_CLIENT.sessionClient().createSession(session, dc);
        System.out.println(session1);
        return session1.getId();
    }
}


@Override
public boolean unlock() {
    final boolean[] flag = {true};
    List<String> dcList = CONSUL_CLIENT.catalogClient().getDatacenters();
    for(String dc: dcList){
        String sessionIdTemp = sessionIdMap.get(dc);
        flag[0] = flag[0] && CONSUL_CLIENT.keyValueClient().releaseLock(lockKey, sessionIdTemp);
        CONSUL_CLIENT.keyValueClient().deleteKey(lockKey);
        CONSUL_CLIENT.sessionClient().destroySession(sessionIdTemp, dc);
        if(!flag[0]){
            logger.warn("try to release lock in dc:{} failed.", dc);
            times.decrementAndGet();
            continue;
        }
    }
    return flag[0];
}

Thank you so much.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant