Java Code Examples for org.apache.bookkeeper.stats.NullStatsLogger#INSTANCE

The following examples show how to use org.apache.bookkeeper.stats.NullStatsLogger#INSTANCE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDistributedLogService.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private DistributedLogServiceImpl createService(
        ServerConfiguration serverConf,
        DistributedLogConfiguration dlConf,
        CountDownLatch latch) throws Exception {
    // Build the stream partition converter
    StreamPartitionConverter converter;
    try {
        converter = ReflectionUtils.newInstance(serverConf.getStreamPartitionConverterClass());
    } catch (ConfigurationException e) {
        logger.warn("Failed to load configured stream-to-partition converter. Fallback to use {}",
                IdentityStreamPartitionConverter.class.getName());
        converter = new IdentityStreamPartitionConverter();
    }
    return new DistributedLogServiceImpl(
            serverConf,
            dlConf,
            ConfUtils.getConstDynConf(dlConf),
            new NullStreamConfigProvider(),
            uri,
            converter,
            NullStatsLogger.INSTANCE,
            NullStatsLogger.INSTANCE,
            latch);
}
 
Example 2
Source File: TestServiceRequestLimiter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
MockHardRequestLimiter(RateLimiter limiter) {
    this.limiter = new ComposableRequestLimiter<MockRequest>(
        limiter,
        new OverlimitFunction<MockRequest>() {
            public void apply(MockRequest request) throws OverCapacityException {
                limitHitCount++;
                throw new OverCapacityException("Limit exceeded");
            }
        },
        new CostFunction<MockRequest>() {
            public int apply(MockRequest request) {
                return request.getSize();
            }
        },
        NullStatsLogger.INSTANCE);
}
 
Example 3
Source File: TestBKLogSegmentWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private BKLogSegmentWriter createLogSegmentWriter(DistributedLogConfiguration conf,
                                                  long logSegmentSequenceNumber,
                                                  long startTxId,
                                                  ZKDistributedLock lock) throws Exception {
    LedgerHandle lh = bkc.get().createLedger(3, 2, 2,
            BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
    return new BKLogSegmentWriter(
            runtime.getMethodName(),
            runtime.getMethodName(),
            conf,
            LogSegmentMetadata.LEDGER_METADATA_CURRENT_LAYOUT_VERSION,
            new BKLogSegmentEntryWriter(lh),
            lock,
            startTxId,
            logSegmentSequenceNumber,
            scheduler,
            NullStatsLogger.INSTANCE,
            NullStatsLogger.INSTANCE,
            new AlertStatsLogger(NullStatsLogger.INSTANCE, "test"),
            PermitLimiter.NULL_PERMIT_LIMITER,
            new SettableFeatureProvider("", 0),
            ConfUtils.getConstDynConf(conf));
}
 
Example 4
Source File: TestServiceRequestLimiter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testDynamicLimiterWithException() throws Exception {
    final AtomicInteger id = new AtomicInteger(0);
    final DynamicDistributedLogConfiguration dynConf = new DynamicDistributedLogConfiguration(
            new ConcurrentConstConfiguration(new DistributedLogConfiguration()));
    DynamicRequestLimiter<MockRequest> limiter = new DynamicRequestLimiter<MockRequest>(
            dynConf, NullStatsLogger.INSTANCE, new SettableFeature("", 0)) {
        @Override
        public RequestLimiter<MockRequest> build() {
            if (id.incrementAndGet() >= 2) {
                throw new RuntimeException("exception in dynamic limiter build()");
            }
            return new MockRequestLimiter();
        }
    };
    limiter.initialize();
    assertEquals(1, id.get());
    try {
        dynConf.setProperty("test1", 1);
        fail("should have thrown on config failure");
    } catch (RuntimeException ex) {
    }
    assertEquals(2, id.get());
}
 
Example 5
Source File: TestServiceRequestLimiter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testDynamicLimiterWithDisabledFeature() throws Exception {
    final DynamicDistributedLogConfiguration dynConf = new DynamicDistributedLogConfiguration(
            new ConcurrentConstConfiguration(new DistributedLogConfiguration()));
    final MockSoftRequestLimiter rateLimiter = new MockSoftRequestLimiter(0);
    final SettableFeature disabledFeature = new SettableFeature("", 0);
    DynamicRequestLimiter<MockRequest> limiter = new DynamicRequestLimiter<MockRequest>(
            dynConf, NullStatsLogger.INSTANCE, disabledFeature) {
        @Override
        public RequestLimiter<MockRequest> build() {
            return rateLimiter;
        }
    };
    limiter.initialize();
    assertEquals(0, rateLimiter.getLimitHitCount());

    // Not disabled, rate limiter was invoked
    limiter.apply(new MockRequest(Integer.MAX_VALUE));
    assertEquals(1, rateLimiter.getLimitHitCount());

    // Disabled, rate limiter not invoked
    disabledFeature.set(1);
    limiter.apply(new MockRequest(Integer.MAX_VALUE));
    assertEquals(1, rateLimiter.getLimitHitCount());
}
 
Example 6
Source File: ZKSessionLock.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
ZKSessionLock(ZooKeeperClient zkClient,
              String lockPath,
              String clientId,
              OrderedScheduler lockStateExecutor)
        throws IOException {
    this(zkClient,
            lockPath,
            clientId,
            lockStateExecutor,
            DistributedLogConstants.LOCK_OP_TIMEOUT_DEFAULT * 1000, NullStatsLogger.INSTANCE,
            new DistributedLockContext());
}
 
Example 7
Source File: BKLogReadHandler.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a Bookkeeper journal manager.
 */
BKLogReadHandler(LogMetadataForReader logMetadata,
                 Optional<String> subscriberId,
                 DistributedLogConfiguration conf,
                 DynamicDistributedLogConfiguration dynConf,
                 LogStreamMetadataStore streamMetadataStore,
                 LogSegmentMetadataCache metadataCache,
                 LogSegmentEntryStore entryStore,
                 OrderedScheduler scheduler,
                 AlertStatsLogger alertStatsLogger,
                 StatsLogger statsLogger,
                 StatsLogger perLogStatsLogger,
                 String clientId,
                 AsyncNotification readerStateNotification,
                 boolean isHandleForReading) {
    super(logMetadata,
            conf,
            streamMetadataStore,
            metadataCache,
            entryStore,
            scheduler,
            statsLogger,
            alertStatsLogger,
            clientId);
    this.logMetadataForReader = logMetadata;
    this.dynConf = dynConf;
    this.perLogStatsLogger =
            isHandleForReading ? perLogStatsLogger : NullStatsLogger.INSTANCE;
    this.readerStateNotification = readerStateNotification;
    this.subscriberId = subscriberId;
}
 
Example 8
Source File: NamespaceBuilder.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static StatsLogger normalizePerLogStatsLogger(StatsLogger statsLogger,
                                                      StatsLogger perLogStatsLogger,
                                                      DistributedLogConfiguration conf) {
    StatsLogger normalizedPerLogStatsLogger = perLogStatsLogger;
    if (perLogStatsLogger == NullStatsLogger.INSTANCE
            && conf.getEnablePerStreamStat()) {
        normalizedPerLogStatsLogger = statsLogger.scope("stream");
    }
    return normalizedPerLogStatsLogger;
}
 
Example 9
Source File: TestBKLogSegmentEntryReader.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
BKLogSegmentEntryReader createEntryReader(LogSegmentMetadata segment,
                                          long startEntryId,
                                          DistributedLogConfiguration conf)
        throws Exception {
    LogSegmentEntryStore store = new BKLogSegmentEntryStore(
            conf,
            ConfUtils.getConstDynConf(conf),
            zkc,
            bkc,
            scheduler,
            null,
            NullStatsLogger.INSTANCE,
            AsyncFailureInjector.NULL);
    return (BKLogSegmentEntryReader) Utils.ioResult(store.openReader(segment, startEntryId));
}
 
Example 10
Source File: BookKeeperDataStorageManagerRestartTest.java    From herddb with Apache License 2.0 5 votes vote down vote up
protected DBManager buildDBManager(String nodeId, Path metadataPath, Path dataPath, Path logsPath, Path tmoDir) {
    ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(),
            testEnv.getTimeout(), testEnv.getPath());
    ServerConfiguration serverConfiguration = new ServerConfiguration();
    BookkeeperCommitLogManager logManager = new BookkeeperCommitLogManager(man, serverConfiguration, NullStatsLogger.INSTANCE);
    BookKeeperDataStorageManager dataManager = new BookKeeperDataStorageManager(nodeId, tmoDir, man, logManager);
    return new DBManager(nodeId, man, dataManager, logManager, tmoDir, null);
}
 
Example 11
Source File: TestBKLogSegmentWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private ZKDistributedLock createLock(String path,
                                     ZooKeeperClient zkClient,
                                     boolean acquireLock)
        throws Exception {
    try {
        Await.result(Utils.zkAsyncCreateFullPathOptimistic(zkClient, path, new byte[0],
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
    } catch (KeeperException.NodeExistsException nee) {
        // node already exists
    }
    SessionLockFactory lockFactory = new ZKSessionLockFactory(
            zkClient,
            "test-lock",
            lockStateExecutor,
            0,
            Long.MAX_VALUE,
            conf.getZKSessionTimeoutMilliseconds(),
            NullStatsLogger.INSTANCE
    );
    ZKDistributedLock lock = new ZKDistributedLock(
            lockStateExecutor,
            lockFactory,
            path,
            Long.MAX_VALUE,
            NullStatsLogger.INSTANCE);
    if (acquireLock) {
        return FutureUtils.result(lock.asyncAcquire());
    } else {
        return lock;
    }
}
 
Example 12
Source File: BookKeeperCommitLogTest.java    From herddb with Apache License 2.0 4 votes vote down vote up
@Test
public void testBookieFailureAsyncWrites() throws Exception {
    final String tableSpaceUUID = UUID.randomUUID().toString();
    final String name = TableSpace.DEFAULT;
    final String nodeid = "nodeid";
    ServerConfiguration serverConfiguration = new ServerConfiguration();
    try (ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(),
            testEnv.getTimeout(), testEnv.getPath());
            BookkeeperCommitLogManager logManager = new BookkeeperCommitLogManager(man, serverConfiguration, NullStatsLogger.INSTANCE)) {
        man.start();
        logManager.start();

        try (BookkeeperCommitLog writer = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
            writer.startWriting();

            // many async writes
            writer.log(LogEntryFactory.beginTransaction(1), false).getLogSequenceNumber();
            writer.log(LogEntryFactory.beginTransaction(1), false).getLogSequenceNumber();
            writer.log(LogEntryFactory.beginTransaction(1), false).getLogSequenceNumber();
            // one sync write
            writer.log(LogEntryFactory.beginTransaction(1), true).getLogSequenceNumber();

            this.testEnv.pauseBookie();
            // this is deemed to fail and we will close the log

            // writer won't see the failure, because this is a deferred write
            writer.log(LogEntryFactory.beginTransaction(2), false).getLogSequenceNumber();

            // this one will fail as well, and we will catch the error, because this is a sync write
            // like a "transaction commit"
            TestUtils.assertThrows(LogNotAvailableException.class, ()
                    -> writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber()
            );

            // no way to recover this instance of BookkeeperCommitLog
            // we will bounce the leader and it will restart with a full recovery
            // in a production env you do not have only one bookie, and the failure
            // of a single bookie will be handled with an ensemble change
            this.testEnv.resumeBookie();

            TestUtils.assertThrows(LogNotAvailableException.class, ()
                    -> writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber()
            );
        }

        // check expected reads
        try (BookkeeperCommitLog reader = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
            List<Map.Entry<LogSequenceNumber, LogEntry>> list = new ArrayList<>();
            reader.recovery(LogSequenceNumber.START_OF_TIME, (lsn, entry) -> {
                if (entry.type != LogEntryType.NOOP) {
                    list.add(new AbstractMap.SimpleImmutableEntry<>(lsn, entry));
                }
            }, false);
            assertEquals(4, list.size());
        }

    }
}
 
Example 13
Source File: DistributedLogManagerFactory.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
public DistributedLogManagerFactory(DistributedLogConfiguration conf, URI uri)
        throws IOException, IllegalArgumentException {
    this(conf, uri, NullStatsLogger.INSTANCE);
}
 
Example 14
Source File: TestDistributedLock.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private void testLockReacquireSuccess(boolean checkOwnershipAndReacquire) throws Exception {
    String lockPath = "/test-lock-re-acquire-success-" + checkOwnershipAndReacquire
            + "-" + System.currentTimeMillis();
    String clientId = "test-lock-re-acquire";

    createLockPath(zkc.get(), lockPath);

    SessionLockFactory lockFactory0 = createLockFactory(clientId, zkc0);
    ZKDistributedLock lock0 =
            new ZKDistributedLock(lockStateExecutor, lockFactory0, lockPath,
                    Long.MAX_VALUE, NullStatsLogger.INSTANCE);
    FutureUtils.result(lock0.asyncAcquire());

    Pair<String, Long> lockId0_1 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();

    List<String> children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    assertEquals(lockId0_1,
            Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));

    ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);

    if (checkOwnershipAndReacquire) {
        checkLockAndReacquire(lock0, true);
        checkLockAndReacquire(lock0, false);
    } else {
        // session expire will trigger lock re-acquisition
        Future<ZKDistributedLock> asyncLockAcquireFuture;
        do {
            Thread.sleep(1);
            asyncLockAcquireFuture = lock0.getLockReacquireFuture();
        } while (null == asyncLockAcquireFuture && lock0.getReacquireCount() < 1);
        if (null != asyncLockAcquireFuture) {
            Await.result(asyncLockAcquireFuture);
        }
        checkLockAndReacquire(lock0, false);
    }
    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    Pair<String, Long> lock0_2 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();
    assertEquals(lock0_2,
            Await.result(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    assertEquals(clientId, lock0_2.getLeft());
    assertFalse(lockId0_1.equals(lock0_2));

    FutureUtils.result(lock0.asyncClose());

    children = getLockWaiters(zkc, lockPath);
    assertEquals(0, children.size());
}
 
Example 15
Source File: DistributedLogServer.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
static Pair<DistributedLogServiceImpl, Server> runServer(
        ServerConfiguration serverConf,
        DistributedLogConfiguration dlConf,
        DynamicDistributedLogConfiguration dynDlConf,
        URI dlUri,
        StreamPartitionConverter partitionConverter,
        StatsProvider provider,
        int port,
        CountDownLatch keepAliveLatch,
        StatsReceiver statsReceiver,
        boolean thriftmux,
        StreamConfigProvider streamConfProvider) throws IOException {
    logger.info("Running server @ uri {}.", dlUri);

    boolean perStreamStatsEnabled = serverConf.isPerStreamStatEnabled();
    StatsLogger perStreamStatsLogger;
    if (perStreamStatsEnabled) {
        perStreamStatsLogger = provider.getStatsLogger("stream");
    } else {
        perStreamStatsLogger = NullStatsLogger.INSTANCE;
    }

    // dl service
    DistributedLogServiceImpl dlService = new DistributedLogServiceImpl(
            serverConf,
            dlConf,
            dynDlConf,
            streamConfProvider,
            dlUri,
            partitionConverter,
            provider.getStatsLogger(""),
            perStreamStatsLogger,
            keepAliveLatch);

    StatsReceiver serviceStatsReceiver = statsReceiver.scope("service");
    StatsLogger serviceStatsLogger = provider.getStatsLogger("service");

    ServerBuilder serverBuilder = ServerBuilder.get()
            .name("DistributedLogServer")
            .codec(ThriftServerFramedCodec.get())
            .reportTo(statsReceiver)
            .keepAlive(true)
            .bindTo(new InetSocketAddress(port));

    if (thriftmux) {
        logger.info("Using thriftmux.");
        Tuple2<Transport.Liveness, Stack.Param<Transport.Liveness>> livenessParam = new Transport.Liveness(
                Duration.Top(), Duration.Top(), Option.apply((Object) Boolean.valueOf(true))).mk();
        serverBuilder = serverBuilder.stack(ThriftMuxServer$.MODULE$.configured(livenessParam._1(), livenessParam._2()));
    }

    logger.info("DistributedLogServer running with the following configuration : \n{}", dlConf.getPropsAsString());

    // starts dl server
    Server server = ServerBuilder.safeBuild(
            new ClientIdRequiredFilter<byte[], byte[]>(serviceStatsReceiver).andThen(
                new StatsFilter<byte[], byte[]>(serviceStatsLogger).andThen(
                    new DistributedLogService.Service(dlService, new TBinaryProtocol.Factory()))),
            serverBuilder);

    logger.info("Started DistributedLog Server.");
    return Pair.of(dlService, server);
}
 
Example 16
Source File: BKDistributedLogNamespace.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public BKDistributedLogNamespace build()
        throws IOException, NullPointerException, IllegalArgumentException {
    Preconditions.checkNotNull(_conf, "No DistributedLog Configuration");
    Preconditions.checkNotNull(_uri, "No DistributedLog URI");
    Preconditions.checkNotNull(_featureProvider, "No Feature Provider");
    Preconditions.checkNotNull(_statsLogger, "No Stats Logger");
    Preconditions.checkNotNull(_featureProvider, "No Feature Provider");
    Preconditions.checkNotNull(_clientId, "No Client ID");
    // validate conf and uri
    validateConfAndURI(_conf, _uri);

    // Build the namespace zookeeper client
    ZooKeeperClientBuilder nsZkcBuilder = createDLZKClientBuilder(
            String.format("dlzk:%s:factory_writer_shared", _uri),
            _conf,
            DLUtils.getZKServersFromDLUri(_uri),
            _statsLogger.scope("dlzk_factory_writer_shared"));
    ZooKeeperClient nsZkc = nsZkcBuilder.build();

    // Resolve namespace binding
    BKDLConfig bkdlConfig = BKDLConfig.resolveDLConfig(nsZkc, _uri);

    // Backward Compatible to enable per log stats by configuration settings
    StatsLogger perLogStatsLogger = _perLogStatsLogger;
    if (perLogStatsLogger == NullStatsLogger.INSTANCE &&
            _conf.getEnablePerStreamStat()) {
        perLogStatsLogger = _statsLogger.scope("stream");
    }

    return new BKDistributedLogNamespace(
            _conf,
            _uri,
            _featureProvider,
            _statsLogger,
            perLogStatsLogger,
            _clientId,
            _regionId,
            nsZkcBuilder,
            nsZkc,
            bkdlConfig);
}
 
Example 17
Source File: BookKeeperCommitLogTest.java    From herddb with Apache License 2.0 4 votes vote down vote up
@Test
public void testBookieFailureSyncWrites() throws Exception {
    final String tableSpaceUUID = UUID.randomUUID().toString();
    final String name = TableSpace.DEFAULT;
    final String nodeid = "nodeid";
    ServerConfiguration serverConfiguration = new ServerConfiguration();
    try (ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(),
            testEnv.getTimeout(), testEnv.getPath());
            BookkeeperCommitLogManager logManager = new BookkeeperCommitLogManager(man, serverConfiguration, NullStatsLogger.INSTANCE)) {
        man.start();
        logManager.start();

        try (BookkeeperCommitLog writer = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
            writer.startWriting();

            writer.log(LogEntryFactory.beginTransaction(1), true).getLogSequenceNumber();

            this.testEnv.pauseBookie();
            // this is deemed to fail and we will close the log
            TestUtils.assertThrows(LogNotAvailableException.class, ()
                    -> writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber()
            );
            assertTrue(writer.isFailed());

            // this one will fail as well
            TestUtils.assertThrows(LogNotAvailableException.class, ()
                    -> writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber()
            );

            // no way to recover this instance of BookkeeperCommitLog
            // we will bounce the leader and it will restart with a full recovery
            // in a production env you do not have only one bookie, and the failure
            // of a single bookie will be handled with an ensemble change
            this.testEnv.resumeBookie();

            TestUtils.assertThrows(LogNotAvailableException.class, ()
                    -> writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber()
            );
        }

        // check expected reads
        try (BookkeeperCommitLog reader = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
            List<Map.Entry<LogSequenceNumber, LogEntry>> list = new ArrayList<>();
            reader.recovery(LogSequenceNumber.START_OF_TIME, (lsn, entry) -> {
                if (entry.type != LogEntryType.NOOP) {
                    list.add(new AbstractMap.SimpleImmutableEntry<>(lsn, entry));
                }
            }, false);
            assertEquals(1, list.size());

            assertTrue(list.get(0).getKey().after(LogSequenceNumber.START_OF_TIME));
        }

    }
}
 
Example 18
Source File: BookKeeperCommitLogTest.java    From herddb with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimpleFence() throws Exception {
    final String tableSpaceUUID = UUID.randomUUID().toString();
    final String name = TableSpace.DEFAULT;
    final String nodeid = "nodeid";
    ServerConfiguration serverConfiguration = new ServerConfiguration();
    try (ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(),
            testEnv.getTimeout(), testEnv.getPath());
            BookkeeperCommitLogManager logManager = new BookkeeperCommitLogManager(man, serverConfiguration, NullStatsLogger.INSTANCE)) {
        man.start();
        logManager.start();

        LogSequenceNumber lsn1;
        LogSequenceNumber lsn2;
        LogSequenceNumber lsn3;
        try (BookkeeperCommitLog writer = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
            writer.startWriting();
            lsn1 = writer.log(LogEntryFactory.beginTransaction(1), true).getLogSequenceNumber();
            lsn2 = writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber();

            // a new leader starts, from START_OF_TIME
            try (BookkeeperCommitLog writer2 = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
                writer2.recovery(LogSequenceNumber.START_OF_TIME, (a, b) -> {}, true);
                writer2.startWriting();
                lsn3 = writer2.log(LogEntryFactory.beginTransaction(3), true).getLogSequenceNumber();
            }

            TestUtils.assertThrows(LogNotAvailableException.class, ()
                    -> FutureUtils.result(writer.log(LogEntryFactory.beginTransaction(3), true).logSequenceNumber));

            assertTrue(writer.isFailed());

            assertTrue(lsn1.after(LogSequenceNumber.START_OF_TIME));
            assertTrue(lsn2.after(lsn1));

            // written by second writer
            assertTrue(lsn3.after(lsn2));
        }

        try (BookkeeperCommitLog reader = logManager.createCommitLog(tableSpaceUUID, name, nodeid);) {
            List<Map.Entry<LogSequenceNumber, LogEntry>> list = new ArrayList<>();
            reader.recovery(LogSequenceNumber.START_OF_TIME, (lsn, entry) -> {
                if (entry.type != LogEntryType.NOOP) {
                    list.add(new AbstractMap.SimpleImmutableEntry<>(lsn, entry));
                }
            }, false);
            assertEquals(3, list.size());
            assertEquals(lsn1, list.get(0).getKey());
            assertEquals(lsn2, list.get(1).getKey());
            assertEquals(lsn3, list.get(2).getKey());
        }

    }
}
 
Example 19
Source File: TestDistributedLock.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 60000)
public void testLockReacquireMultiple() throws Exception {
    String lockPath = "/reacquirePathMultiple";
    Utils.zkCreateFullPathOptimistic(zkc, lockPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
        CreateMode.PERSISTENT);
    String clientId = "lockHolder";
    SessionLockFactory factory = createLockFactory(clientId, zkc, conf.getLockTimeoutMilliSeconds(), 0);
    ZKDistributedLock lock = new ZKDistributedLock(lockStateExecutor, factory, lockPath,
        conf.getLockTimeoutMilliSeconds(), NullStatsLogger.INSTANCE);
    Utils.ioResult(lock.asyncAcquire());

    // try and cleanup the underlying lock
    lock.getInternalLock().unlock();

    // This should reacquire the lock
    checkLockAndReacquire(lock, true);

    assertEquals(true, lock.haveLock());
    assertEquals(true, lock.getInternalLock().isLockHeld());

    factory = createLockFactory(clientId + "_2", zkc, 0, 0);
    ZKDistributedLock lock2 = new ZKDistributedLock(lockStateExecutor, factory, lockPath,
        0, NullStatsLogger.INSTANCE);

    boolean exceptionEncountered = false;
    try {
        Utils.ioResult(lock2.asyncAcquire());
    } catch (OwnershipAcquireFailedException exc) {
        assertEquals(clientId, exc.getCurrentOwner());
        exceptionEncountered = true;
    }
    assertTrue(exceptionEncountered);
    Utils.ioResult(lock2.asyncClose());

    Utils.ioResult(lock.asyncClose());
    assertEquals(false, lock.haveLock());
    assertEquals(false, lock.getInternalLock().isLockHeld());

    factory = createLockFactory(clientId + "_3", zkc, 0, 0);
    ZKDistributedLock lock3 = new ZKDistributedLock(lockStateExecutor, factory, lockPath,
        0, NullStatsLogger.INSTANCE);

    Utils.ioResult(lock3.asyncAcquire());
    assertEquals(true, lock3.haveLock());
    assertEquals(true, lock3.getInternalLock().isLockHeld());
    Utils.ioResult(lock3.asyncClose());
}
 
Example 20
Source File: ZooKeeperClient.java    From distributedlog with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an unconnected client that will lazily attempt to connect on the first call to
 * {@link #get}.  All successful connections will be authenticated with the given
 * {@code credentials}.
 *
 * @param sessionTimeoutMs
 *          ZK session timeout in milliseconds
 * @param connectionTimeoutMs
 *          ZK connection timeout in milliseconds
 * @param zooKeeperServers
 *          the set of servers forming the ZK cluster
 */
ZooKeeperClient(int sessionTimeoutMs, int connectionTimeoutMs, String zooKeeperServers) {
    this("default", sessionTimeoutMs, connectionTimeoutMs, zooKeeperServers, null, NullStatsLogger.INSTANCE, 1, 0,
         Credentials.NONE);
}