Search in sources :

Example 6 with BadVersionException

use of org.apache.zookeeper.KeeperException.BadVersionException in project Saturn by vipshop.

the class ExecutorCleanService method updateJobConfigPreferListContentToRemoveDeletedExecutor.

/**
 * delete $Jobs/job/config/preferList content about xxx
 */
private String updateJobConfigPreferListContentToRemoveDeletedExecutor(String jobName, String executorName) throws KeeperException.ConnectionLossException, InterruptedException {
    try {
        String jobConfigPreferListNodePath = SaturnExecutorsNode.getJobConfigPreferListNodePath(jobName);
        Stat stat = new Stat();
        byte[] jobConfigPreferListNodeBytes = curatorFramework.getData().storingStatIn(stat).forPath(jobConfigPreferListNodePath);
        if (jobConfigPreferListNodeBytes == null || jobConfigPreferListNodeBytes.length == 0) {
            return null;
        }
        // build the new prefer list string
        StringBuilder sb = new StringBuilder();
        String[] split = new String(jobConfigPreferListNodeBytes, "UTF-8").split(",");
        boolean found = false;
        for (String tmp : split) {
            String tmpTrim = tmp.trim();
            if (!tmpTrim.equals(executorName)) {
                if (sb.length() > 0) {
                    sb.append(',');
                }
                sb.append(tmpTrim);
            } else {
                found = true;
            }
        }
        curatorFramework.setData().withVersion(stat.getVersion()).forPath(jobConfigPreferListNodePath, sb.toString().getBytes("UTF-8"));
        return found ? sb.toString() : null;
    } catch (NoNodeException | BadVersionException e) {
    // NOSONAR
    // ignore
    } catch (KeeperException.ConnectionLossException | InterruptedException e) {
        throw e;
    } catch (Exception e) {
        log.error("Clean the executor, updateJobConfigPreferListContentToRemoveDeletedExecutor(" + jobName + ", " + executorName + ") error", e);
    }
    return null;
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) KeeperException(org.apache.zookeeper.KeeperException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) Stat(org.apache.zookeeper.data.Stat)

Example 7 with BadVersionException

use of org.apache.zookeeper.KeeperException.BadVersionException in project accumulo by apache.

the class ZooReaderWriterTest method testMutateWithRetryOnSetData.

@Test
public void testMutateWithRetryOnSetData() throws Exception {
    final String path = "/foo";
    final byte[] value = { 0 };
    final byte[] mutatedBytes = { 1 };
    Mutator mutator = currentValue -> mutatedBytes;
    zrw = createMockBuilder(ZooReaderWriter.class).addMockedMethods("getRetryFactory", "getZooKeeper").createMock();
    expect(zrw.getRetryFactory()).andReturn(retryFactory).anyTimes();
    expect(zrw.getZooKeeper()).andReturn(zk).anyTimes();
    Stat stat = new Stat();
    zk.create(path, value, ZooUtil.PUBLIC, CreateMode.PERSISTENT);
    expectLastCall().andThrow(new NodeExistsException()).once();
    expect(zk.getData(path, null, stat)).andReturn(new byte[] { 3 }).times(2);
    // BadVersionException should retry
    expect(zk.setData(path, mutatedBytes, 0)).andThrow(new ConnectionLossException());
    expect(retry.canRetry()).andReturn(true);
    retry.useRetry();
    expectLastCall();
    retry.waitForNextAttempt();
    expectLastCall();
    // Let 2nd setData succeed
    expect(zk.setData(path, mutatedBytes, 0)).andReturn(null);
    replay(zk, zrw, retryFactory, retry);
    assertArrayEquals(new byte[] { 1 }, zrw.mutateOrCreate(path, value, mutator));
    verify(zk, zrw, retryFactory, retry);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RetryFactory(org.apache.accumulo.fate.util.Retry.RetryFactory) CreateMode(org.apache.zookeeper.CreateMode) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) ZooKeeper(org.apache.zookeeper.ZooKeeper) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) BeforeEach(org.junit.jupiter.api.BeforeEach) EasyMock.createMockBuilder(org.easymock.EasyMock.createMockBuilder) Retry(org.apache.accumulo.fate.util.Retry) KeeperException(org.apache.zookeeper.KeeperException) Mutator(org.apache.accumulo.fate.zookeeper.ZooReaderWriter.Mutator) EasyMock.expect(org.easymock.EasyMock.expect) Stat(org.apache.zookeeper.data.Stat) SessionExpiredException(org.apache.zookeeper.KeeperException.SessionExpiredException) EasyMock.expectLastCall(org.easymock.EasyMock.expectLastCall) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) Test(org.junit.jupiter.api.Test) EasyMock.replay(org.easymock.EasyMock.replay) EasyMock.createMock(org.easymock.EasyMock.createMock) EasyMock.verify(org.easymock.EasyMock.verify) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) Code(org.apache.zookeeper.KeeperException.Code) Stat(org.apache.zookeeper.data.Stat) Mutator(org.apache.accumulo.fate.zookeeper.ZooReaderWriter.Mutator) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) Test(org.junit.jupiter.api.Test)

Aggregations

BadVersionException (org.apache.zookeeper.KeeperException.BadVersionException)7 KeeperException (org.apache.zookeeper.KeeperException)5 Stat (org.apache.zookeeper.data.Stat)4 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)3 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)2 RollingUpdateOp (com.spotify.helios.rollingupdate.RollingUpdateOp)2 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)2 ZooKeeperOperation (com.spotify.helios.servicescommon.coordination.ZooKeeperOperation)2 IOException (java.io.IOException)2 Collections.emptyMap (java.util.Collections.emptyMap)2 Map (java.util.Map)2 Retry (org.apache.accumulo.fate.util.Retry)2 RetryFactory (org.apache.accumulo.fate.util.Retry.RetryFactory)2 Mutator (org.apache.accumulo.fate.zookeeper.ZooReaderWriter.Mutator)2 CreateMode (org.apache.zookeeper.CreateMode)2 Code (org.apache.zookeeper.KeeperException.Code)2 ConnectionLossException (org.apache.zookeeper.KeeperException.ConnectionLossException)2 SessionExpiredException (org.apache.zookeeper.KeeperException.SessionExpiredException)2