Search in sources :

Example 1 with SimplePermitLimiter

use of org.apache.distributedlog.util.SimplePermitLimiter in project bookkeeper by apache.

the class NamespaceBuilder method build.

/**
 * Build the namespace.
 *
 * @return the namespace instance.
 * @throws IllegalArgumentException when there is illegal argument provided in the builder
 * @throws NullPointerException when there is null argument provided in the builder
 * @throws IOException when fail to build the backend
 */
public Namespace build() throws IllegalArgumentException, NullPointerException, IOException {
    // Check arguments
    checkNotNull(_conf, "No DistributedLog Configuration.");
    checkNotNull(_uri, "No DistributedLog URI");
    // validate the configuration
    _conf.validate();
    if (null == _dynConf) {
        _dynConf = ConfUtils.getConstDynConf(_conf);
    }
    // retrieve the namespace driver
    NamespaceDriver driver = NamespaceDriverManager.getDriver(_uri);
    URI normalizedUri = DLUtils.normalizeURI(_uri);
    // build the feature provider
    FeatureProvider featureProvider;
    if (null == _featureProvider) {
        featureProvider = new SettableFeatureProvider("", 0);
        logger.info("No feature provider is set. All features are disabled now.");
    } else {
        featureProvider = _featureProvider;
    }
    // build the failure injector
    AsyncFailureInjector failureInjector = AsyncRandomFailureInjector.newBuilder().injectDelays(_conf.getEIInjectReadAheadDelay(), _conf.getEIInjectReadAheadDelayPercent(), _conf.getEIInjectMaxReadAheadDelayMs()).injectErrors(false, 10).injectStops(_conf.getEIInjectReadAheadStall(), 10).injectCorruption(_conf.getEIInjectReadAheadBrokenEntries()).build();
    // normalize the per log stats logger
    StatsLogger perLogStatsLogger = normalizePerLogStatsLogger(_statsLogger, _perLogStatsLogger, _conf);
    // build the scheduler
    OrderedScheduler scheduler = OrderedScheduler.newSchedulerBuilder().name("DLM-" + normalizedUri.getPath()).numThreads(_conf.getNumWorkerThreads()).build();
    // initialize the namespace driver
    driver.initialize(_conf, _dynConf, normalizedUri, scheduler, featureProvider, failureInjector, _statsLogger, perLogStatsLogger, DLUtils.normalizeClientId(_clientId), _regionId);
    // initialize the write limiter
    PermitLimiter writeLimiter;
    if (_conf.getGlobalOutstandingWriteLimit() < 0) {
        writeLimiter = PermitLimiter.NULL_PERMIT_LIMITER;
    } else {
        Feature disableWriteLimitFeature = featureProvider.getFeature(CoreFeatureKeys.DISABLE_WRITE_LIMIT.name().toLowerCase());
        writeLimiter = new SimplePermitLimiter(_conf.getOutstandingWriteLimitDarkmode(), _conf.getGlobalOutstandingWriteLimit(), _statsLogger.scope("writeLimiter"), true, /* singleton */
        disableWriteLimitFeature);
    }
    return new BKDistributedLogNamespace(_conf, normalizedUri, driver, scheduler, featureProvider, writeLimiter, failureInjector, _statsLogger, perLogStatsLogger, DLUtils.normalizeClientId(_clientId), _regionId);
}
Also used : AsyncFailureInjector(org.apache.distributedlog.injector.AsyncFailureInjector) NullStatsLogger(org.apache.bookkeeper.stats.NullStatsLogger) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) BKDistributedLogNamespace(org.apache.distributedlog.BKDistributedLogNamespace) NamespaceDriver(org.apache.distributedlog.namespace.NamespaceDriver) FeatureProvider(org.apache.bookkeeper.feature.FeatureProvider) SettableFeatureProvider(org.apache.bookkeeper.feature.SettableFeatureProvider) SimplePermitLimiter(org.apache.distributedlog.util.SimplePermitLimiter) PermitLimiter(org.apache.distributedlog.common.util.PermitLimiter) SimplePermitLimiter(org.apache.distributedlog.util.SimplePermitLimiter) URI(java.net.URI) Feature(org.apache.bookkeeper.feature.Feature) SettableFeatureProvider(org.apache.bookkeeper.feature.SettableFeatureProvider) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler)

Example 2 with SimplePermitLimiter

use of org.apache.distributedlog.util.SimplePermitLimiter in project bookkeeper by apache.

the class TestAsyncReaderWriter method writeRecordsWithOutstandingWriteLimit.

public void writeRecordsWithOutstandingWriteLimit(int stream, int global, boolean shouldFail) throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(true);
    confLocal.setPerWriterOutstandingWriteLimit(stream);
    confLocal.setOutstandingWriteLimitDarkmode(false);
    DistributedLogManager dlm;
    if (global > -1) {
        dlm = createNewDLM(confLocal, runtime.getMethodName(), new SimplePermitLimiter(false, global, new NullStatsLogger(), true, new FixedValueFeature("", 0)));
    } else {
        dlm = createNewDLM(confLocal, runtime.getMethodName());
    }
    BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm.startAsyncLogSegmentNonPartitioned());
    ArrayList<CompletableFuture<DLSN>> results = new ArrayList<CompletableFuture<DLSN>>(1000);
    for (int i = 0; i < 1000; i++) {
        results.add(writer.write(DLMTestUtil.getLogRecordInstance(1L)));
    }
    for (CompletableFuture<DLSN> result : results) {
        try {
            Utils.ioResult(result);
            if (shouldFail) {
                fail("should fail due to no outstanding writes permitted");
            }
        } catch (OverCapacityException ex) {
            assertTrue(shouldFail);
        }
    }
    writer.closeAndComplete();
    dlm.close();
}
Also used : FixedValueFeature(org.apache.bookkeeper.feature.FixedValueFeature) ArrayList(java.util.ArrayList) DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) NullStatsLogger(org.apache.bookkeeper.stats.NullStatsLogger) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) SimplePermitLimiter(org.apache.distributedlog.util.SimplePermitLimiter) OverCapacityException(org.apache.distributedlog.exceptions.OverCapacityException)

Example 3 with SimplePermitLimiter

use of org.apache.distributedlog.util.SimplePermitLimiter in project bookkeeper by apache.

the class TestWriteLimiter method testDarkmodeWithDisabledFeature.

@Test(timeout = 60000)
public void testDarkmodeWithDisabledFeature() throws Exception {
    SettableFeature feature = new SettableFeature("test", 10000);
    SimplePermitLimiter streamLimiter = createPermitLimiter(true, 1, feature);
    SimplePermitLimiter globalLimiter = createPermitLimiter(true, Integer.MAX_VALUE, feature);
    WriteLimiter limiter = new WriteLimiter("test", streamLimiter, globalLimiter);
    limiter.acquire();
    limiter.acquire();
    assertPermits(streamLimiter, 2, globalLimiter, 2);
    limiter.release();
    limiter.release();
    assertPermits(streamLimiter, 0, globalLimiter, 0);
}
Also used : SimplePermitLimiter(org.apache.distributedlog.util.SimplePermitLimiter) SettableFeature(org.apache.bookkeeper.feature.SettableFeature) Test(org.junit.Test)

Example 4 with SimplePermitLimiter

use of org.apache.distributedlog.util.SimplePermitLimiter in project bookkeeper by apache.

the class TestWriteLimiter method testSetDisableFeatureAfterAcquireAndBeforeRelease.

@Test(timeout = 60000)
public void testSetDisableFeatureAfterAcquireAndBeforeRelease() throws Exception {
    SettableFeature feature = new SettableFeature("test", 0);
    SimplePermitLimiter streamLimiter = createPermitLimiter(false, 2, feature);
    SimplePermitLimiter globalLimiter = createPermitLimiter(false, Integer.MAX_VALUE, feature);
    WriteLimiter limiter = new WriteLimiter("test", streamLimiter, globalLimiter);
    limiter.acquire();
    limiter.acquire();
    assertPermits(streamLimiter, 2, globalLimiter, 2);
    feature.set(10000);
    limiter.release();
    limiter.release();
    assertPermits(streamLimiter, 0, globalLimiter, 0);
}
Also used : SimplePermitLimiter(org.apache.distributedlog.util.SimplePermitLimiter) SettableFeature(org.apache.bookkeeper.feature.SettableFeature) Test(org.junit.Test)

Example 5 with SimplePermitLimiter

use of org.apache.distributedlog.util.SimplePermitLimiter in project bookkeeper by apache.

the class TestWriteLimiter method testDarkmodeGlobalUnderStreamOver.

@Test(timeout = 60000)
public void testDarkmodeGlobalUnderStreamOver() throws Exception {
    SimplePermitLimiter streamLimiter = createPermitLimiter(true, 1);
    SimplePermitLimiter globalLimiter = createPermitLimiter(true, 2);
    WriteLimiter limiter = new WriteLimiter("test", streamLimiter, globalLimiter);
    limiter.acquire();
    limiter.acquire();
    assertPermits(streamLimiter, 2, globalLimiter, 2);
    limiter.release();
    limiter.release();
    assertPermits(streamLimiter, 0, globalLimiter, 0);
}
Also used : SimplePermitLimiter(org.apache.distributedlog.util.SimplePermitLimiter) Test(org.junit.Test)

Aggregations

SimplePermitLimiter (org.apache.distributedlog.util.SimplePermitLimiter)12 Test (org.junit.Test)10 SettableFeature (org.apache.bookkeeper.feature.SettableFeature)5 OverCapacityException (org.apache.distributedlog.exceptions.OverCapacityException)5 NullStatsLogger (org.apache.bookkeeper.stats.NullStatsLogger)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 OrderedScheduler (org.apache.bookkeeper.common.util.OrderedScheduler)1 Feature (org.apache.bookkeeper.feature.Feature)1 FeatureProvider (org.apache.bookkeeper.feature.FeatureProvider)1 FixedValueFeature (org.apache.bookkeeper.feature.FixedValueFeature)1 SettableFeatureProvider (org.apache.bookkeeper.feature.SettableFeatureProvider)1 StatsLogger (org.apache.bookkeeper.stats.StatsLogger)1 BKDistributedLogNamespace (org.apache.distributedlog.BKDistributedLogNamespace)1 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)1 PermitLimiter (org.apache.distributedlog.common.util.PermitLimiter)1 DynamicDistributedLogConfiguration (org.apache.distributedlog.config.DynamicDistributedLogConfiguration)1 AsyncFailureInjector (org.apache.distributedlog.injector.AsyncFailureInjector)1 NamespaceDriver (org.apache.distributedlog.namespace.NamespaceDriver)1