DL-17: Ledger allocator pool should be able to use hostname to distinguish write proxies
…guish write proxies
Author: Jay Juma <jayk.juma@gmail.com>
Reviewers: Sijie Guo <sijie@apache.org>
Closes #86 from jayk-juma/jay/use_hostname_for_ledger_allocation
diff --git a/distributedlog-service/src/main/java/com/twitter/distributedlog/service/DistributedLogServiceImpl.java b/distributedlog-service/src/main/java/com/twitter/distributedlog/service/DistributedLogServiceImpl.java
index 3a9b904..f529927 100644
--- a/distributedlog-service/src/main/java/com/twitter/distributedlog/service/DistributedLogServiceImpl.java
+++ b/distributedlog-service/src/main/java/com/twitter/distributedlog/service/DistributedLogServiceImpl.java
@@ -51,6 +51,7 @@
import com.twitter.distributedlog.service.stream.WriteOp;
import com.twitter.distributedlog.service.stream.limiter.ServiceRequestLimiter;
import com.twitter.distributedlog.service.streamset.StreamPartitionConverter;
+import com.twitter.distributedlog.service.utils.ServerUtils;
import com.twitter.distributedlog.thrift.service.BulkWriteResponse;
import com.twitter.distributedlog.thrift.service.ClientInfo;
import com.twitter.distributedlog.thrift.service.DistributedLogService;
@@ -163,7 +164,10 @@
int shard = serverConf.getServerShardId();
int numThreads = serverConf.getServerThreads();
this.clientId = DLSocketAddress.toLockId(DLSocketAddress.getSocketAddress(serverPort), shard);
- String allocatorPoolName = String.format("allocator_%04d_%010d", serverRegionId, shard);
+ String allocatorPoolName = ServerUtils.getLedgerAllocatorPoolName(
+ serverRegionId,
+ shard,
+ serverConf.isUseHostnameAsAllocatorPoolName());
dlConf.setLedgerAllocatorPoolName(allocatorPoolName);
this.featureProvider = AbstractFeatureProvider.getFeatureProvider("", dlConf, statsLogger.scope("features"));
if (this.featureProvider instanceof AbstractFeatureProvider) {
diff --git a/distributedlog-service/src/main/java/com/twitter/distributedlog/service/config/ServerConfiguration.java b/distributedlog-service/src/main/java/com/twitter/distributedlog/service/config/ServerConfiguration.java
index 09661c0..9a9e83c 100644
--- a/distributedlog-service/src/main/java/com/twitter/distributedlog/service/config/ServerConfiguration.java
+++ b/distributedlog-service/src/main/java/com/twitter/distributedlog/service/config/ServerConfiguration.java
@@ -91,6 +91,11 @@
// Server stream to partition converter
protected final static String SERVER_STREAM_PARTITION_CONVERTER_CLASS = "stream_partition_converter_class";
+ // Use hostname as the allocator pool name
+ protected static final String SERVER_USE_HOSTNAME_AS_ALLOCATOR_POOL_NAME
+ = "server_use_hostname_as_allocator_pool_name";
+ protected static final boolean SERVER_USE_HOSTNAME_AS_ALLOCATOR_POOL_NAME_DEFAULT = false;
+
public ServerConfiguration() {
super();
addConfiguration(new SystemConfiguration());
@@ -370,6 +375,30 @@
defaultLoader);
}
+ /**
+ * Set if use hostname as the allocator pool name.
+ *
+ * @param useHostname whether to use hostname as the allocator pool name.
+ * @return server configuration
+ * @see #isUseHostnameAsAllocatorPoolName()
+ */
+ public ServerConfiguration setUseHostnameAsAllocatorPoolName(boolean useHostname) {
+ setProperty(SERVER_USE_HOSTNAME_AS_ALLOCATOR_POOL_NAME, useHostname);
+ return this;
+ }
+
+ /**
+ * Get if use hostname as the allocator pool name
+ *
+ * @return true if use hostname as the allocator pool name. otherwise, use
+ * {@link #getServerShardId()} as the allocator pool name.
+ * @see #getServerShardId()
+ */
+ public boolean isUseHostnameAsAllocatorPoolName() {
+ return getBoolean(SERVER_USE_HOSTNAME_AS_ALLOCATOR_POOL_NAME,
+ SERVER_USE_HOSTNAME_AS_ALLOCATOR_POOL_NAME_DEFAULT);
+ }
+
/**
* Validate the configuration
*/
diff --git a/distributedlog-service/src/main/java/com/twitter/distributedlog/service/utils/ServerUtils.java b/distributedlog-service/src/main/java/com/twitter/distributedlog/service/utils/ServerUtils.java
new file mode 100644
index 0000000..af65b11
--- /dev/null
+++ b/distributedlog-service/src/main/java/com/twitter/distributedlog/service/utils/ServerUtils.java
@@ -0,0 +1,49 @@
+/**
+ * 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 com.twitter.distributedlog.service.utils;
+
+import java.io.IOException;
+import java.net.InetAddress;
+
+/**
+ * Utils that used by servers.
+ */
+public class ServerUtils {
+
+ /**
+ * Retrieve the ledger allocator pool name.
+ *
+ * @param serverRegionId region id that that server is running
+ * @param shardId shard id of the server
+ * @param useHostname whether to use hostname as the ledger allocator pool name
+ * @return ledger allocator pool name
+ * @throws IOException
+ */
+ public static String getLedgerAllocatorPoolName(int serverRegionId,
+ int shardId,
+ boolean useHostname)
+ throws IOException {
+ if (useHostname) {
+ return String.format("allocator_%04d_%s", serverRegionId,
+ InetAddress.getLocalHost().getHostAddress());
+ } else {
+ return String.format("allocator_%04d_%010d", serverRegionId, shardId);
+ }
+ }
+
+}
diff --git a/distributedlog-service/src/test/java/com/twitter/distributedlog/service/config/TestServerConfiguration.java b/distributedlog-service/src/test/java/com/twitter/distributedlog/service/config/TestServerConfiguration.java
index 5aebd7b..c76dcb6 100644
--- a/distributedlog-service/src/test/java/com/twitter/distributedlog/service/config/TestServerConfiguration.java
+++ b/distributedlog-service/src/test/java/com/twitter/distributedlog/service/config/TestServerConfiguration.java
@@ -51,4 +51,12 @@
conf.validate();
}
+ @Test(timeout = 60000)
+ public void testUseHostnameAsAllocatorPoolName() {
+ ServerConfiguration conf = new ServerConfiguration();
+ assertFalse("Should not use hostname by default", conf.isUseHostnameAsAllocatorPoolName());
+ conf.setUseHostnameAsAllocatorPoolName(true);
+ assertTrue("Should use hostname now", conf.isUseHostnameAsAllocatorPoolName());
+ }
+
}
diff --git a/distributedlog-service/src/test/java/com/twitter/distributedlog/service/utils/TestServerUtils.java b/distributedlog-service/src/test/java/com/twitter/distributedlog/service/utils/TestServerUtils.java
new file mode 100644
index 0000000..776205c
--- /dev/null
+++ b/distributedlog-service/src/test/java/com/twitter/distributedlog/service/utils/TestServerUtils.java
@@ -0,0 +1,41 @@
+/**
+ * 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 com.twitter.distributedlog.service.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import java.net.InetAddress;
+import org.junit.Test;
+
+/**
+ * Test Case for {@link ServerUtils}.
+ */
+public class TestServerUtils {
+
+ @Test(timeout = 6000)
+ public void testGetLedgerAllocatorPoolName() throws Exception {
+ int region = 123;
+ int shard = 999;
+ String hostname = InetAddress.getLocalHost().getHostAddress();
+ assertEquals("allocator_0123_0000000999",
+ ServerUtils.getLedgerAllocatorPoolName(region, shard, false));
+ assertEquals("allocator_0123_" + hostname,
+ ServerUtils.getLedgerAllocatorPoolName(region, shard, true));
+ }
+
+}