Skip to content

Commit

Permalink
Fix the 'BadVersion' exception handling in doSet() code path. (apache…
Browse files Browse the repository at this point in the history
  • Loading branch information
desaikomal authored and himanshukandwal committed Sep 16, 2023
1 parent ed384f2 commit d5f491e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ public AccessResult doSet(String path, T record, int expectVersion, int options)
result._retCode = RetCode.ERROR;
return result;
}
} catch (ZkBadVersionException e) {
LOG.debug("Exception while setting path: " + path, e);
throw e;
} catch (Exception e) {
LOG.error("Exception while setting path: " + path, e);
result._retCode = RetCode.ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.helix.BaseDataAccessor;
import org.apache.helix.PropertyPathBuilder;
import org.apache.helix.TestHelper;
import org.apache.helix.zookeeper.api.client.HelixZkClient;
import org.apache.helix.zookeeper.api.client.RealmAwareZkClient;
import org.apache.helix.zookeeper.datamodel.ZNRecord;
import org.apache.helix.zookeeper.datamodel.ZNRecordUpdater;
Expand All @@ -38,6 +39,7 @@
import org.apache.helix.manager.zk.ZkBaseDataAccessor.RetCode;
import org.apache.helix.zookeeper.exception.ZkClientException;
import org.apache.helix.zookeeper.zkclient.DataUpdater;
import org.apache.helix.zookeeper.zkclient.exception.ZkBadVersionException;
import org.apache.helix.zookeeper.zkclient.exception.ZkException;
import org.apache.helix.zookeeper.zkclient.exception.ZkMarshallingError;
import org.apache.helix.zookeeper.zkclient.serialize.ZkSerializer;
Expand Down Expand Up @@ -182,6 +184,35 @@ public void testSyncDoSet() {
System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
}

@Test
public void testDoSetWithException() {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String testName = className + "_" + methodName;

System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis()));

String path = String.format("/%s/%s/%s", _rootPath, "msg_0", "submsg_0");
ZNRecord record = new ZNRecord("submsg_0");
ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
AccessResult result = accessor.doSet(path, record, -1, AccessOption.PERSISTENT);
ZNRecord getRecord = _gZkClient.readData(path);

// create mock spy for _gZkClient
HelixZkClient mockZkClient = Mockito.spy(_gZkClient);

// mock so that _gZkClient throws ZkBadVersionException
Mockito.doThrow(new ZkBadVersionException(""))
.when(mockZkClient).writeDataGetStat(Mockito.anyString(), Mockito.any(), Mockito.anyInt());

try {
accessor.doSet(path, record, getRecord.getVersion(), AccessOption.PERSISTENT);
} catch (ZkBadVersionException e) {
// OK
}
System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
}

@Test
public void testSyncCreate() {
String className = TestHelper.getTestClassName();
Expand Down

0 comments on commit d5f491e

Please sign in to comment.