Search in sources :

Example 1 with Mutator

use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.

the class ZooReaderWriterTest method testMutateWithBadVersion.

@Test
public void testMutateWithBadVersion() throws Exception {
    final String path = "/foo";
    final byte[] value = new byte[] { 0 };
    final List<ACL> acls = Collections.emptyList();
    final byte[] mutatedBytes = new byte[] { 1 };
    Mutator mutator = new Mutator() {

        @Override
        public byte[] mutate(byte[] currentValue) throws Exception {
            return mutatedBytes;
        }
    };
    Method getDataMethod = ZooReaderWriter.class.getMethod("getData", String.class, boolean.class, Stat.class);
    zrw = EasyMock.createMockBuilder(ZooReaderWriter.class).addMockedMethods("getRetryFactory", "getZooKeeper").addMockedMethod(getDataMethod).createMock();
    EasyMock.expect(zrw.getRetryFactory()).andReturn(retryFactory).anyTimes();
    EasyMock.expect(zrw.getZooKeeper()).andReturn(zk).anyTimes();
    Stat stat = new Stat();
    zk.create(path, value, acls, CreateMode.PERSISTENT);
    EasyMock.expectLastCall().andThrow(new NodeExistsException()).once();
    EasyMock.expect(zrw.getData(path, false, stat)).andReturn(new byte[] { 3 }).times(2);
    // BadVersionException should retry
    EasyMock.expect(zk.setData(path, mutatedBytes, 0)).andThrow(new BadVersionException());
    // Let 2nd setData succeed
    EasyMock.expect(zk.setData(path, mutatedBytes, 0)).andReturn(null);
    EasyMock.replay(zk, zrw, retryFactory, retry);
    Assert.assertArrayEquals(new byte[] { 1 }, zrw.mutate(path, value, acls, mutator));
    EasyMock.verify(zk, zrw, retryFactory, retry);
}
Also used : Stat(org.apache.zookeeper.data.Stat) Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) ACL(org.apache.zookeeper.data.ACL) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 2 with Mutator

use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.

the class ZooReaderWriterTest method testMutateNodeCreationFails.

@Test(expected = SessionExpiredException.class)
public void testMutateNodeCreationFails() throws Exception {
    final String path = "/foo";
    final byte[] value = new byte[] { 0 };
    final List<ACL> acls = Collections.emptyList();
    Mutator mutator = new Mutator() {

        @Override
        public byte[] mutate(byte[] currentValue) throws Exception {
            return new byte[] { 1 };
        }
    };
    zk.create(path, value, acls, CreateMode.PERSISTENT);
    EasyMock.expectLastCall().andThrow(new SessionExpiredException()).once();
    EasyMock.expect(retry.canRetry()).andReturn(false);
    EasyMock.expect(retry.retriesCompleted()).andReturn(1l).once();
    EasyMock.replay(zk, zrw, retryFactory, retry);
    zrw.mutate(path, value, acls, mutator);
}
Also used : Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) ACL(org.apache.zookeeper.data.ACL) SessionExpiredException(org.apache.zookeeper.KeeperException.SessionExpiredException) Test(org.junit.Test)

Example 3 with Mutator

use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.

the class RenameNamespace method call.

@Override
public Repo<Master> call(long id, Master master) throws Exception {
    Instance instance = master.getInstance();
    IZooReaderWriter zoo = ZooReaderWriter.getInstance();
    Utils.tableNameLock.lock();
    try {
        Utils.checkNamespaceDoesNotExist(instance, newName, namespaceId, TableOperation.RENAME);
        final String tap = ZooUtil.getRoot(instance) + Constants.ZNAMESPACES + "/" + namespaceId + Constants.ZNAMESPACE_NAME;
        zoo.mutate(tap, null, null, new Mutator() {

            @Override
            public byte[] mutate(byte[] current) throws Exception {
                final String currentName = new String(current);
                if (currentName.equals(newName))
                    // assume in this case the operation is running again, so we are done
                    return null;
                if (!currentName.equals(oldName)) {
                    throw new AcceptableThriftTableOperationException(null, oldName, TableOperation.RENAME, TableOperationExceptionType.NAMESPACE_NOTFOUND, "Name changed while processing");
                }
                return newName.getBytes();
            }
        });
        Tables.clearCache(instance);
    } finally {
        Utils.tableNameLock.unlock();
        Utils.unreserveNamespace(namespaceId, id, true);
    }
    LoggerFactory.getLogger(RenameNamespace.class).debug("Renamed namespace {} {} {}", namespaceId, oldName, newName);
    return null;
}
Also used : Instance(org.apache.accumulo.core.client.Instance) Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) IZooReaderWriter(org.apache.accumulo.fate.zookeeper.IZooReaderWriter) AcceptableThriftTableOperationException(org.apache.accumulo.core.client.impl.AcceptableThriftTableOperationException) AcceptableThriftTableOperationException(org.apache.accumulo.core.client.impl.AcceptableThriftTableOperationException)

Example 4 with Mutator

use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.

the class CompactRange method call.

@Override
public Repo<Master> call(final long tid, Master env) throws Exception {
    String zTablePath = Constants.ZROOT + "/" + env.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID;
    IZooReaderWriter zoo = ZooReaderWriter.getInstance();
    byte[] cid;
    try {
        cid = zoo.mutate(zTablePath, null, null, new Mutator() {

            @Override
            public byte[] mutate(byte[] currentValue) throws Exception {
                String cvs = new String(currentValue, UTF_8);
                String[] tokens = cvs.split(",");
                long flushID = Long.parseLong(tokens[0]);
                flushID++;
                String txidString = String.format("%016x", tid);
                for (int i = 1; i < tokens.length; i++) {
                    if (tokens[i].startsWith(txidString))
                        // skip self
                        continue;
                    log.debug("txidString : {}", txidString);
                    log.debug("tokens[{}] : {}", i, tokens[i]);
                    throw new AcceptableThriftTableOperationException(tableId.canonicalID(), null, TableOperation.COMPACT, TableOperationExceptionType.OTHER, "Another compaction with iterators and/or a compaction strategy is running");
                }
                StringBuilder encodedIterators = new StringBuilder();
                if (config != null) {
                    Hex hex = new Hex();
                    encodedIterators.append(",");
                    encodedIterators.append(txidString);
                    encodedIterators.append("=");
                    encodedIterators.append(new String(hex.encode(config), UTF_8));
                }
                return (Long.toString(flushID) + encodedIterators).getBytes(UTF_8);
            }
        });
        return new CompactionDriver(Long.parseLong(new String(cid, UTF_8).split(",")[0]), namespaceId, tableId, startRow, endRow);
    } catch (NoNodeException nne) {
        throw new AcceptableThriftTableOperationException(tableId.canonicalID(), null, TableOperation.COMPACT, TableOperationExceptionType.NOTFOUND, null);
    }
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) IZooReaderWriter(org.apache.accumulo.fate.zookeeper.IZooReaderWriter) Hex(org.apache.commons.codec.binary.Hex) AcceptableThriftTableOperationException(org.apache.accumulo.core.client.impl.AcceptableThriftTableOperationException)

Example 5 with Mutator

use of org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator in project accumulo by apache.

the class CompactRange method removeIterators.

static void removeIterators(Master environment, final long txid, Table.ID tableId) throws Exception {
    String zTablePath = Constants.ZROOT + "/" + environment.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID;
    IZooReaderWriter zoo = ZooReaderWriter.getInstance();
    zoo.mutate(zTablePath, null, null, new Mutator() {

        @Override
        public byte[] mutate(byte[] currentValue) throws Exception {
            String cvs = new String(currentValue, UTF_8);
            String[] tokens = cvs.split(",");
            long flushID = Long.parseLong(tokens[0]);
            String txidString = String.format("%016x", txid);
            StringBuilder encodedIterators = new StringBuilder();
            for (int i = 1; i < tokens.length; i++) {
                if (tokens[i].startsWith(txidString))
                    continue;
                encodedIterators.append(",");
                encodedIterators.append(tokens[i]);
            }
            return (Long.toString(flushID) + encodedIterators).getBytes(UTF_8);
        }
    });
}
Also used : Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) IZooReaderWriter(org.apache.accumulo.fate.zookeeper.IZooReaderWriter) AcceptableThriftTableOperationException(org.apache.accumulo.core.client.impl.AcceptableThriftTableOperationException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException)

Aggregations

Mutator (org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator)9 IZooReaderWriter (org.apache.accumulo.fate.zookeeper.IZooReaderWriter)6 AcceptableThriftTableOperationException (org.apache.accumulo.core.client.impl.AcceptableThriftTableOperationException)4 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)3 ACL (org.apache.zookeeper.data.ACL)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 Instance (org.apache.accumulo.core.client.Instance)2 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)2 Stat (org.apache.zookeeper.data.Stat)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 AccumuloException (org.apache.accumulo.core.client.AccumuloException)1 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)1 NamespaceNotFoundException (org.apache.accumulo.core.client.NamespaceNotFoundException)1 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)1 Namespace (org.apache.accumulo.core.client.impl.Namespace)1 Table (org.apache.accumulo.core.client.impl.Table)1 ThriftSecurityException (org.apache.accumulo.core.client.impl.thrift.ThriftSecurityException)1 ThriftTableOperationException (org.apache.accumulo.core.client.impl.thrift.ThriftTableOperationException)1 MetadataTable (org.apache.accumulo.core.metadata.MetadataTable)1