Skip to content

Commit 7e7fec5

Browse files
authored
Added name for thread pool (#342)
## Description of change This PR overrides the thread names for AAL thread pool and sets threads as daemon threads #### Relevant issues N/A #### Does this contribution introduce any breaking changes to the existing APIs or behaviors? No #### Does this contribution introduce any new public APIs or behaviors? No #### How was the contribution tested? Unit tests #### Does this contribution need a changelog entry? N/A --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and I agree to the terms of the [Developer Certificate of Origin (DCO)](https://developercertificate.org/).
1 parent 30393bd commit 7e7fec5

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

input-stream/src/main/java/software/amazon/s3/analyticsaccelerator/S3SeekableInputStreamFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import software.amazon.s3.analyticsaccelerator.io.physical.impl.PhysicalIOImpl;
3737
import software.amazon.s3.analyticsaccelerator.request.ObjectClient;
3838
import software.amazon.s3.analyticsaccelerator.request.ObjectMetadata;
39+
import software.amazon.s3.analyticsaccelerator.util.NamedThreadFactory;
3940
import software.amazon.s3.analyticsaccelerator.util.ObjectFormatSelector;
4041
import software.amazon.s3.analyticsaccelerator.util.OpenStreamInformation;
4142
import software.amazon.s3.analyticsaccelerator.util.S3URI;
@@ -62,6 +63,7 @@ public class S3SeekableInputStreamFactory implements AutoCloseable {
6263
private final ExecutorService threadPool;
6364

6465
private static final Logger LOG = LoggerFactory.getLogger(S3SeekableInputStreamFactory.class);
66+
private static final String THREAD_FACTORY_NAME = "s3-analytics-accelerator-";
6567

6668
/**
6769
* Creates a new instance of {@link S3SeekableInputStreamFactory}. This factory should be used to
@@ -87,7 +89,8 @@ public S3SeekableInputStreamFactory(
8789
// TODO: calling applications should be able to pass in a thread pool if they so wish
8890
this.threadPool =
8991
Executors.newFixedThreadPool(
90-
configuration.getPhysicalIOConfiguration().getThreadPoolSize());
92+
configuration.getPhysicalIOConfiguration().getThreadPoolSize(),
93+
new NamedThreadFactory(THREAD_FACTORY_NAME, true));
9194
this.objectBlobStore =
9295
new BlobStore(
9396
objectClient,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.amazon.s3.analyticsaccelerator.util;
17+
18+
import java.util.concurrent.ThreadFactory;
19+
import java.util.concurrent.atomic.AtomicInteger;
20+
21+
/** ThreadFactory that creates named threads with configurable daemon status. */
22+
public class NamedThreadFactory implements ThreadFactory {
23+
private final AtomicInteger threadNumber = new AtomicInteger(1);
24+
private final String namePrefix;
25+
private final boolean daemon;
26+
27+
/**
28+
* Constructor
29+
*
30+
* @param namePrefix name
31+
* @param daemon is daemon
32+
*/
33+
public NamedThreadFactory(String namePrefix, boolean daemon) {
34+
this.namePrefix = namePrefix;
35+
this.daemon = daemon;
36+
}
37+
38+
@Override
39+
public Thread newThread(Runnable r) {
40+
Thread t = new Thread(r, namePrefix + threadNumber.getAndIncrement());
41+
t.setDaemon(daemon);
42+
return t;
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.amazon.s3.analyticsaccelerator.util;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
class NamedThreadFactoryTest {
24+
25+
@Test
26+
void testThreadNaming() {
27+
NamedThreadFactory factory = new NamedThreadFactory("test-", true);
28+
29+
Thread thread1 = factory.newThread(() -> {});
30+
Thread thread2 = factory.newThread(() -> {});
31+
32+
assertEquals("test-1", thread1.getName());
33+
assertEquals("test-2", thread2.getName());
34+
}
35+
36+
@Test
37+
void testDaemonThread() {
38+
NamedThreadFactory factory = new NamedThreadFactory("daemon-", true);
39+
40+
Thread thread = factory.newThread(() -> {});
41+
42+
assertTrue(thread.isDaemon());
43+
}
44+
}

0 commit comments

Comments
 (0)