Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.bookkeeper.common.util.MathUtils;
import org.apache.bookkeeper.common.util.Watcher;
import org.apache.bookkeeper.common.util.nativeio.NativeIOImpl;
import org.apache.bookkeeper.conf.DbLedgerStorageConfiguration;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.meta.LedgerManager;
import org.apache.bookkeeper.slogger.slf4j.Slf4jSlogger;
Expand All @@ -78,40 +79,6 @@
@Slf4j
public class DbLedgerStorage implements LedgerStorage {

public static final String WRITE_CACHE_MAX_SIZE_MB = "dbStorage_writeCacheMaxSizeMb";
public static final String READ_AHEAD_CACHE_MAX_SIZE_MB = "dbStorage_readAheadCacheMaxSizeMb";
public static final String DIRECT_IO_ENTRYLOGGER = "dbStorage_directIOEntryLogger";
public static final String DIRECT_IO_ENTRYLOGGER_TOTAL_WRITEBUFFER_SIZE_MB =
"dbStorage_directIOEntryLoggerTotalWriteBufferSizeMb";
public static final String DIRECT_IO_ENTRYLOGGER_TOTAL_READBUFFER_SIZE_MB =
"dbStorage_directIOEntryLoggerTotalReadBufferSizeMb";
public static final String DIRECT_IO_ENTRYLOGGER_READBUFFER_SIZE_MB =
"dbStorage_directIOEntryLoggerReadBufferSizeMb";
public static final String DIRECT_IO_ENTRYLOGGER_MAX_FD_CACHE_TIME_SECONDS =
"dbStorage_directIOEntryLoggerMaxFdCacheTimeSeconds";

static final String MAX_THROTTLE_TIME_MILLIS = "dbStorage_maxThrottleTimeMs";

private static final int MB = 1024 * 1024;

private static final long DEFAULT_WRITE_CACHE_MAX_SIZE_MB =
(long) (0.25 * PlatformDependent.estimateMaxDirectMemory()) / MB;
private static final long DEFAULT_READ_CACHE_MAX_SIZE_MB =
(long) (0.25 * PlatformDependent.estimateMaxDirectMemory()) / MB;

static final String READ_AHEAD_CACHE_BATCH_SIZE = "dbStorage_readAheadCacheBatchSize";
private static final int DEFAULT_READ_AHEAD_CACHE_BATCH_SIZE = 100;

private static final long DEFAULT_DIRECT_IO_TOTAL_WRITEBUFFER_SIZE_MB =
(long) (0.125 * PlatformDependent.estimateMaxDirectMemory())
/ MB;
private static final long DEFAULT_DIRECT_IO_TOTAL_READBUFFER_SIZE_MB =
(long) (0.125 * PlatformDependent.estimateMaxDirectMemory())
/ MB;
private static final long DEFAULT_DIRECT_IO_READBUFFER_SIZE_MB = 8;

private static final int DEFAULT_DIRECT_IO_MAX_FD_CACHE_TIME_SECONDS = 300;

// use the storage assigned to ledger 0 for flags.
// if the storage configuration changes, the flags may be lost
// but in that case data integrity should kick off anyhow.
Expand Down Expand Up @@ -146,19 +113,18 @@ public class DbLedgerStorage implements LedgerStorage {
public void initialize(ServerConfiguration conf, LedgerManager ledgerManager, LedgerDirsManager ledgerDirsManager,
LedgerDirsManager indexDirsManager, StatsLogger statsLogger, ByteBufAllocator allocator)
throws IOException {
long writeCacheMaxSize = getLongVariableOrDefault(conf, WRITE_CACHE_MAX_SIZE_MB,
DEFAULT_WRITE_CACHE_MAX_SIZE_MB) * MB;
long readCacheMaxSize = getLongVariableOrDefault(conf, READ_AHEAD_CACHE_MAX_SIZE_MB,
DEFAULT_READ_CACHE_MAX_SIZE_MB) * MB;
boolean directIOEntryLogger = getBooleanVariableOrDefault(conf, DIRECT_IO_ENTRYLOGGER, false);
DbLedgerStorageConfiguration dbLedgerStorageConf = conf.toDbLedgerStorageConfiguration();
long writeCacheMaxSize = dbLedgerStorageConf.getWriteCacheMaxSize();
long readCacheMaxSize = dbLedgerStorageConf.getReadCacheMaxSize();
boolean directIOEntryLogger = dbLedgerStorageConf.isDirectIOEntryLoggerEnabled();

this.allocator = allocator;
this.numberOfDirs = ledgerDirsManager.getAllLedgerDirs().size();

log.info("Started Db Ledger Storage");
log.info(" - Number of directories: {}", numberOfDirs);
log.info(" - Write cache size: {} MB", writeCacheMaxSize / MB);
log.info(" - Read Cache: {} MB", readCacheMaxSize / MB);
log.info(" - Write cache size: {} MB", writeCacheMaxSize / 1024 / 1024);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we define MB ?

log.info(" - Read Cache: {} MB", readCacheMaxSize / 1024 / 1024);

if (readCacheMaxSize + writeCacheMaxSize > PlatformDependent.estimateMaxDirectMemory()) {
throw new IOException("Read and write cache sizes exceed the configured max direct memory size");
Expand All @@ -170,7 +136,7 @@ public void initialize(ServerConfiguration conf, LedgerManager ledgerManager, Le

long perDirectoryWriteCacheSize = writeCacheMaxSize / numberOfDirs;
long perDirectoryReadCacheSize = readCacheMaxSize / numberOfDirs;
int readAheadCacheBatchSize = conf.getInt(READ_AHEAD_CACHE_BATCH_SIZE, DEFAULT_READ_AHEAD_CACHE_BATCH_SIZE);
int readAheadCacheBatchSize = dbLedgerStorageConf.getReadAheadCacheBatchSize();

gcExecutor = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("GarbageCollector"));

Expand All @@ -194,22 +160,15 @@ public void initialize(ServerConfiguration conf, LedgerManager ledgerManager, Le

EntryLogger entrylogger;
if (directIOEntryLogger) {
long perDirectoryTotalWriteBufferSize = MB * getLongVariableOrDefault(
conf,
DIRECT_IO_ENTRYLOGGER_TOTAL_WRITEBUFFER_SIZE_MB,
DEFAULT_DIRECT_IO_TOTAL_WRITEBUFFER_SIZE_MB) / numberOfDirs;
long perDirectoryTotalReadBufferSize = MB * getLongVariableOrDefault(
conf,
DIRECT_IO_ENTRYLOGGER_TOTAL_READBUFFER_SIZE_MB,
DEFAULT_DIRECT_IO_TOTAL_READBUFFER_SIZE_MB) / numberOfDirs;
int readBufferSize = MB * (int) getLongVariableOrDefault(
conf,
DIRECT_IO_ENTRYLOGGER_READBUFFER_SIZE_MB,
DEFAULT_DIRECT_IO_READBUFFER_SIZE_MB);
int maxFdCacheTimeSeconds = (int) getLongVariableOrDefault(
conf,
DIRECT_IO_ENTRYLOGGER_MAX_FD_CACHE_TIME_SECONDS,
DEFAULT_DIRECT_IO_MAX_FD_CACHE_TIME_SECONDS);
long perDirectoryTotalWriteBufferSize =
dbLedgerStorageConf.getDirectIOEntryLoggerTotalWriteBufferSize() / numberOfDirs;
long perDirectoryTotalReadBufferSize =
dbLedgerStorageConf.getDirectIOEntryLoggerTotalReadBufferSize() / numberOfDirs;

int readBufferSize = (int) dbLedgerStorageConf.getDirectIOEntryLoggerReadBufferSize();

int maxFdCacheTimeSeconds = (int) dbLedgerStorageConf.getDirectIOEntryLoggerMaxFDCacheTimeSeconds();

Slf4jSlogger slog = new Slf4jSlogger(DbLedgerStorage.class);
entryLoggerWriteExecutor = Executors.newSingleThreadExecutor(
new DefaultThreadFactory("EntryLoggerWrite"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ public class SingleDirectoryDbLedgerStorage implements CompactableLedgerStorage

private final DbLedgerStorageStats dbLedgerStorageStats;

private static final long DEFAULT_MAX_THROTTLE_TIME_MILLIS = TimeUnit.SECONDS.toMillis(10);

private final long maxReadAheadBytesSize;

private final Counter flushExecutorTime;
Expand Down Expand Up @@ -175,8 +173,7 @@ public SingleDirectoryDbLedgerStorage(ServerConfiguration conf, LedgerManager le
// Do not attempt to perform read-ahead more than half the total size of the cache
maxReadAheadBytesSize = readCacheMaxSize / 2;

long maxThrottleTimeMillis = conf.getLong(DbLedgerStorage.MAX_THROTTLE_TIME_MILLIS,
DEFAULT_MAX_THROTTLE_TIME_MILLIS);
long maxThrottleTimeMillis = conf.toDbLedgerStorageConfiguration().getMaxThrottleTimeMillis();
maxThrottleTimeNanos = TimeUnit.MILLISECONDS.toNanos(maxThrottleTimeMillis);

readCache = new ReadCache(allocator, readCacheMaxSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.bookkeeper.conf;

// CHECKSTYLE.OFF: IllegalImport
import com.google.common.annotations.VisibleForTesting;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.util.NettyRuntime;
import io.netty.util.internal.PlatformDependent;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
// CHECKSTYLE.ON: IllegalImport

public class DbLedgerStorageConfiguration extends ServerConfiguration {

private static final int MB = 1024 * 1024;

@VisibleForTesting
public static final String WRITE_CACHE_MAX_SIZE_MB = "dbStorage_writeCacheMaxSizeMb";

private static final long DEFAULT_WRITE_CACHE_MAX_SIZE_MB =
(long) (0.25 * PlatformDependent.estimateMaxDirectMemory()) / MB;

@VisibleForTesting
public static final String READ_AHEAD_CACHE_MAX_SIZE_MB = "dbStorage_readAheadCacheMaxSizeMb";

private static final long DEFAULT_READ_CACHE_MAX_SIZE_MB =
(long) (0.25 * PlatformDependent.estimateMaxDirectMemory()) / MB;

private static final String READ_AHEAD_CACHE_BATCH_SIZE = "dbStorage_readAheadCacheBatchSize";

private static final int DEFAULT_READ_AHEAD_CACHE_BATCH_SIZE = 100;

private static final String DIRECT_IO_ENTRYLOGGER = "dbStorage_directIOEntryLogger";

private static final String DIRECT_IO_ENTRYLOGGER_TOTAL_WRITEBUFFER_SIZE_MB =
"dbStorage_directIOEntryLoggerTotalWriteBufferSizeMb";

private static final long DEFAULT_DIRECT_IO_TOTAL_WRITEBUFFER_SIZE_MB =
(long) (0.125 * PlatformDependent.estimateMaxDirectMemory()) / MB;

private static final String DIRECT_IO_ENTRYLOGGER_TOTAL_READBUFFER_SIZE_MB =
"dbStorage_directIOEntryLoggerTotalReadBufferSizeMb";

private static final long DEFAULT_DIRECT_IO_TOTAL_READBUFFER_SIZE_MB =
(long) (0.125 * PlatformDependent.estimateMaxDirectMemory()) / MB;

private static final String DIRECT_IO_ENTRYLOGGER_READBUFFER_SIZE_MB =
"dbStorage_directIOEntryLoggerReadBufferSizeMb";

private static final long DEFAULT_DIRECT_IO_READBUFFER_SIZE_MB = 8;

private static final String DIRECT_IO_ENTRYLOGGER_MAX_FD_CACHE_TIME_SECONDS =
"dbStorage_directIOEntryLoggerMaxFdCacheTimeSeconds";

private static final int DEFAULT_DIRECT_IO_MAX_FD_CACHE_TIME_SECONDS = 300;

@VisibleForTesting
public static final String MAX_THROTTLE_TIME_MILLIS = "dbStorage_maxThrottleTimeMs";

private static final long DEFAULT_MAX_THROTTLE_TIME_MILLIS = TimeUnit.SECONDS.toMillis(10);

public long getWriteCacheMaxSize() {
return getLongVariableOrDefault(WRITE_CACHE_MAX_SIZE_MB, DEFAULT_WRITE_CACHE_MAX_SIZE_MB) * MB;
}

public long getReadCacheMaxSize() {
return getLongVariableOrDefault(READ_AHEAD_CACHE_MAX_SIZE_MB, DEFAULT_READ_CACHE_MAX_SIZE_MB) * MB;
}

public int getReadAheadCacheBatchSize() {
return this.getInt(READ_AHEAD_CACHE_BATCH_SIZE, DEFAULT_READ_AHEAD_CACHE_BATCH_SIZE);
}

public boolean isDirectIOEntryLoggerEnabled() {
return getBooleanVariableOrDefault(DIRECT_IO_ENTRYLOGGER, false);
}

public long getDirectIOEntryLoggerTotalWriteBufferSize() {
return getLongVariableOrDefault(DIRECT_IO_ENTRYLOGGER_TOTAL_WRITEBUFFER_SIZE_MB,
DEFAULT_DIRECT_IO_TOTAL_WRITEBUFFER_SIZE_MB) * MB;
}

public long getDirectIOEntryLoggerTotalReadBufferSize() {
return getLongVariableOrDefault(DIRECT_IO_ENTRYLOGGER_TOTAL_READBUFFER_SIZE_MB,
DEFAULT_DIRECT_IO_TOTAL_READBUFFER_SIZE_MB) * MB;
}

public long getDirectIOEntryLoggerReadBufferSize() {
return getLongVariableOrDefault(DIRECT_IO_ENTRYLOGGER_READBUFFER_SIZE_MB, DEFAULT_DIRECT_IO_READBUFFER_SIZE_MB)
* MB;
}

public long getDirectIOEntryLoggerMaxFDCacheTimeSeconds() {
return getLongVariableOrDefault(DIRECT_IO_ENTRYLOGGER_MAX_FD_CACHE_TIME_SECONDS,
DEFAULT_DIRECT_IO_MAX_FD_CACHE_TIME_SECONDS);
}

public long getMaxThrottleTimeMillis() {
return this.getLong(MAX_THROTTLE_TIME_MILLIS, DEFAULT_MAX_THROTTLE_TIME_MILLIS);
}

/**
* The configured pooling concurrency for the allocator, if user config it, we should consider the unpooled direct
* memory which readCache and writeCache occupy when use DbLedgerStorage.
*/
public int getAllocatorPoolingConcurrency() {
long writeCacheSize = this.getWriteCacheMaxSize();
long readCacheSize = this.getReadCacheMaxSize();
long availableDirectMemory = PlatformDependent.maxDirectMemory() - writeCacheSize - readCacheSize;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use PlatformDependent.estimateMaxDirectMemory() instead of PlatformDependent.maxDirectMemory(). Please refer to: #2989

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In dbLedgerStorage,

  • If we use DefaultEntryLogger, only the whole write-cache will be allocated at one time in the init state, and the read-cache will be allocated on-demand.
  • If we use DirectEnryLogger, both the write-cache and the read-cache are allocated on-demand.

So I think we'd better keep the default implementation.

int defaultMinNumArena = NettyRuntime.availableProcessors() * 2;
final int defaultChunkSize =
PooledByteBufAllocator.defaultPageSize() << PooledByteBufAllocator.defaultMaxOrder();
int suitableNum = (int) (availableDirectMemory / defaultChunkSize / 2 / 3);
return Math.min(defaultMinNumArena, suitableNum);
}

private long getLongVariableOrDefault(String keyName, long defaultValue) {
Object obj = this.getProperty(keyName);
if (obj instanceof Number) {
return ((Number) obj).longValue();
} else if (obj == null) {
return defaultValue;
} else if (StringUtils.isEmpty(this.getString(keyName))) {
return defaultValue;
} else {
return this.getLong(keyName);
}
}

private boolean getBooleanVariableOrDefault(String keyName, boolean defaultValue) {
Object obj = this.getProperty(keyName);
if (obj instanceof Boolean) {
return (Boolean) obj;
} else if (obj == null) {
return defaultValue;
} else if (StringUtils.isEmpty(this.getString(keyName))) {
return defaultValue;
} else {
return this.getBoolean(keyName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4012,4 +4012,19 @@ public ServerConfiguration setLedgerMetadataRocksdbConf(String ledgerMetadataRoc
this.setProperty(LEDGER_METADATA_ROCKSDB_CONF, ledgerMetadataRocksdbConf);
return this;
}

public DbLedgerStorageConfiguration toDbLedgerStorageConfiguration() {
DbLedgerStorageConfiguration dbLedgerStorageConfiguration = new DbLedgerStorageConfiguration();
dbLedgerStorageConfiguration.copy(this);
return dbLedgerStorageConfiguration;
}

public int getAllocatorPoolingConcurrency() {
String ledgerStorageClass = getLedgerStorageClass();
if (DbLedgerStorage.class.getName().equals(ledgerStorageClass)) {
return toDbLedgerStorageConfiguration().getAllocatorPoolingConcurrency();
}
return super.getAllocatorPoolingConcurrency();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.bookkeeper.client.BookKeeper.DigestType;
import org.apache.bookkeeper.client.LedgerHandle;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.conf.DbLedgerStorageConfiguration;
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
import org.junit.Test;
import org.slf4j.Logger;
Expand All @@ -44,10 +45,10 @@ public DbLedgerStorageBookieTest() {
baseConf.setGcWaitTime(60000);

// Leave it empty to pickup default
baseConf.setProperty(DbLedgerStorage.WRITE_CACHE_MAX_SIZE_MB, "");
baseConf.setProperty(DbLedgerStorageConfiguration.WRITE_CACHE_MAX_SIZE_MB, "");

// Configure explicitly with a int object
baseConf.setProperty(DbLedgerStorage.READ_AHEAD_CACHE_MAX_SIZE_MB, 16);
baseConf.setProperty(DbLedgerStorageConfiguration.READ_AHEAD_CACHE_MAX_SIZE_MB, 16);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.bookkeeper.bookie.BookieException;
import org.apache.bookkeeper.bookie.BookieImpl;
import org.apache.bookkeeper.bookie.TestBookieImpl;
import org.apache.bookkeeper.conf.DbLedgerStorageConfiguration;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.conf.TestBKConfiguration;
import org.apache.bookkeeper.proto.BookieProtocol;
Expand Down Expand Up @@ -72,8 +73,8 @@ public void setup() throws Exception {
conf.setGcWaitTime(gcWaitTime);
/** the testcase cover specify indexDir for the class {@link SingleDirectoryDbLedgerStorage} */
conf.setLedgerStorageClass(DbLedgerStorage.class.getName());
conf.setProperty(DbLedgerStorage.WRITE_CACHE_MAX_SIZE_MB, 1);
conf.setProperty(DbLedgerStorage.MAX_THROTTLE_TIME_MILLIS, 1000);
conf.setProperty(DbLedgerStorageConfiguration.WRITE_CACHE_MAX_SIZE_MB, 1);
conf.setProperty(DbLedgerStorageConfiguration.MAX_THROTTLE_TIME_MILLIS, 1000);
conf.setLedgerDirNames(new String[]{tmpLedgerDir.toString()});
conf.setIndexDirName(new String[]{tmpIndexDir.toString()});
Bookie bookie = new TestBookieImpl(conf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.bookkeeper.bookie.LedgerStorage;
import org.apache.bookkeeper.bookie.TestBookieImpl;
import org.apache.bookkeeper.bookie.storage.EntryLogger;
import org.apache.bookkeeper.conf.DbLedgerStorageConfiguration;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.conf.TestBKConfiguration;
import org.apache.bookkeeper.proto.BookieProtocol;
Expand Down Expand Up @@ -251,8 +252,8 @@ public void doubleDirectory() throws Exception {
File secondDir = new File(tmpDir, "dir2");
ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
conf.setGcWaitTime(gcWaitTime);
conf.setProperty(DbLedgerStorage.WRITE_CACHE_MAX_SIZE_MB, 4);
conf.setProperty(DbLedgerStorage.READ_AHEAD_CACHE_MAX_SIZE_MB, 4);
conf.setProperty(DbLedgerStorageConfiguration.WRITE_CACHE_MAX_SIZE_MB, 4);
conf.setProperty(DbLedgerStorageConfiguration.READ_AHEAD_CACHE_MAX_SIZE_MB, 4);
conf.setLedgerStorageClass(DbLedgerStorage.class.getName());
conf.setLedgerDirNames(new String[] { firstDir.getCanonicalPath(), secondDir.getCanonicalPath() });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.bookkeeper.bookie.LedgerDirsManager;
import org.apache.bookkeeper.bookie.TestBookieImpl;
import org.apache.bookkeeper.bookie.storage.EntryLogger;
import org.apache.bookkeeper.conf.DbLedgerStorageConfiguration;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.conf.TestBKConfiguration;
import org.apache.bookkeeper.meta.LedgerManager;
Expand Down Expand Up @@ -112,8 +113,8 @@ public void setup() throws Exception {
ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
conf.setGcWaitTime(gcWaitTime);
conf.setLedgerStorageClass(MockedDbLedgerStorage.class.getName());
conf.setProperty(DbLedgerStorage.WRITE_CACHE_MAX_SIZE_MB, 1);
conf.setProperty(DbLedgerStorage.MAX_THROTTLE_TIME_MILLIS, 1000);
conf.setProperty(DbLedgerStorageConfiguration.WRITE_CACHE_MAX_SIZE_MB, 1);
conf.setProperty(DbLedgerStorageConfiguration.MAX_THROTTLE_TIME_MILLIS, 1000);
conf.setLedgerDirNames(new String[] { tmpDir.toString() });
Bookie bookie = new TestBookieImpl(conf);

Expand Down
Loading