Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZOOKEEPER-4743: Increase data version once more when going back to -1 from Integer.MIN_VALUE #2064

Merged
merged 6 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1962,4 +1962,18 @@ public static StatPersisted createStat(long zxid, long time, long ephemeralOwner
stat.setEphemeralOwner(ephemeralOwner);
return stat;
}

// for test only
public static StatPersisted createStat(int version) {
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
StatPersisted stat = new StatPersisted();
stat.setCtime(0);
stat.setMtime(0);
stat.setCzxid(0);
stat.setMzxid(0);
stat.setPzxid(0);
stat.setVersion(version);
stat.setAversion(0);
stat.setEphemeralOwner(0);
return stat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,18 @@ private String getParentPathAndValidate(String path) throws BadArgumentsExceptio
}

private static int checkAndIncVersion(int currentVersion, int expectedVersion, String path) throws KeeperException.BadVersionException {
if (expectedVersion != -1 && expectedVersion != currentVersion) {
throw new KeeperException.BadVersionException(path);
if (expectedVersion != -1) {
if (expectedVersion != currentVersion) {
throw new KeeperException.BadVersionException(path);
}
if (expectedVersion == -2) {
return 0;
} else {
return currentVersion + 1;
}
} else {
return currentVersion + 1;
zhaohaidao marked this conversation as resolved.
Show resolved Hide resolved
}
return currentVersion + 1;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooDefs.OpCode;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.proto.CreateRequest;
import org.apache.zookeeper.proto.ReconfigRequest;
import org.apache.zookeeper.proto.RequestHeader;
Expand All @@ -59,6 +60,7 @@
import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier;
import org.apache.zookeeper.test.ClientBase;
import org.apache.zookeeper.txn.ErrorTxn;
import org.apache.zookeeper.txn.SetDataTxn;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -391,4 +393,110 @@ public Set<Long> localSessions() {
return Collections.emptySet();
}
}

@Test
public void testCheckAndIncVersion() throws Exception {
zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0);
pLatch = new CountDownLatch(1);
processor = new PrepRequestProcessor(zks, new MyRequestProcessor());

SetDataRequest record = new SetDataRequest("/foo", new byte[0], 0);
Request req = createRequest(record, OpCode.setData, false);
processor.pRequest(req);
pLatch.await();
assertEquals(OpCode.setData, outcome.getHdr().getType());
assertTrue(outcome.getTxn() instanceof SetDataTxn);
SetDataTxn setDataTxn = (SetDataTxn) outcome.getTxn();
assertEquals(1, setDataTxn.getVersion());
}

@Test
public void testCheckAndIncVersionOverflow() throws Exception {
Stat customStat = new Stat();
customStat.setVersion(Integer.MAX_VALUE);
zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0);
DataNode node = zks.getZKDatabase().dataTree.getNode("/foo");
node.stat = DataTree.createStat(Integer.MAX_VALUE);

pLatch = new CountDownLatch(1);
processor = new PrepRequestProcessor(zks, new MyRequestProcessor());

SetDataRequest record = new SetDataRequest("/foo", new byte[0], Integer.MAX_VALUE);
Request req = createRequest(record, OpCode.setData, false);
processor.pRequest(req);
pLatch.await();
assertEquals(OpCode.setData, outcome.getHdr().getType());
assertTrue(outcome.getTxn() instanceof SetDataTxn);
SetDataTxn setDataTxn = (SetDataTxn) outcome.getTxn();
assertEquals(Integer.MIN_VALUE, setDataTxn.getVersion());
}
@Test
public void testCheckAndIncVersionWithNegativeNumber() throws Exception {
zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0);
DataNode node = zks.getZKDatabase().dataTree.getNode("/foo");
node.stat = DataTree.createStat(Integer.MIN_VALUE);

pLatch = new CountDownLatch(1);
processor = new PrepRequestProcessor(zks, new MyRequestProcessor());

SetDataRequest record = new SetDataRequest("/foo", new byte[0], Integer.MIN_VALUE);
Request req = createRequest(record, OpCode.setData, false);
processor.pRequest(req);
pLatch.await();
assertEquals(OpCode.setData, outcome.getHdr().getType());
assertTrue(outcome.getTxn() instanceof SetDataTxn);
SetDataTxn setDataTxn = (SetDataTxn) outcome.getTxn();
assertEquals(Integer.MIN_VALUE + 1, setDataTxn.getVersion());
}

@Test
public void testCheckAndIncToZeroFromNegativeTwo() throws Exception {
zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0);
DataNode node = zks.getZKDatabase().dataTree.getNode("/foo");
node.stat = DataTree.createStat(-2);

pLatch = new CountDownLatch(1);
processor = new PrepRequestProcessor(zks, new MyRequestProcessor());

SetDataRequest record = new SetDataRequest("/foo", new byte[0], -2);
Request req = createRequest(record, OpCode.setData, false);
processor.pRequest(req);
pLatch.await();
assertEquals(OpCode.setData, outcome.getHdr().getType());
assertTrue(outcome.getTxn() instanceof SetDataTxn);
SetDataTxn setDataTxn = (SetDataTxn) outcome.getTxn();
assertEquals(0, setDataTxn.getVersion());
}

@Test
public void testCheckAndIncSkipEqualityCheck() throws Exception {
zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0);
DataNode node = zks.getZKDatabase().dataTree.getNode("/foo");

pLatch = new CountDownLatch(1);
processor = new PrepRequestProcessor(zks, new MyRequestProcessor());

SetDataRequest record = new SetDataRequest("/foo", new byte[0], -1);
Request req = createRequest(record, OpCode.setData, false);
processor.pRequest(req);
pLatch.await();
assertEquals(OpCode.setData, outcome.getHdr().getType());
assertTrue(outcome.getTxn() instanceof SetDataTxn);
SetDataTxn setDataTxn = (SetDataTxn) outcome.getTxn();
assertEquals(1, setDataTxn.getVersion());
}

@Test
public void testCheckAndIncWithBadVersion() throws Exception {
zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0);
pLatch = new CountDownLatch(1);
processor = new PrepRequestProcessor(zks, new MyRequestProcessor());

SetDataRequest record = new SetDataRequest("/foo", new byte[0], 1);
Request req = createRequest(record, OpCode.setData, false);
processor.pRequest(req);
pLatch.await();
assertEquals(OpCode.error, outcome.getHdr().getType());
assertEquals(KeeperException.Code.BADVERSION, outcome.getException().code());
}
}