-
Notifications
You must be signed in to change notification settings - Fork 526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HDDS-12547. Container creation and import use the same VolumeChoosingPolicy #8090
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,8 @@ public final class VolumeChoosingPolicyFactory { | |
private static final Class<? extends VolumeChoosingPolicy> | ||
DEFAULT_VOLUME_CHOOSING_POLICY = CapacityVolumeChoosingPolicy.class; | ||
|
||
private static volatile VolumeChoosingPolicy policyInstance; | ||
|
||
private VolumeChoosingPolicyFactory() { | ||
} | ||
|
||
|
@@ -41,4 +43,24 @@ public static VolumeChoosingPolicy getPolicy(ConfigurationSource conf) | |
DEFAULT_VOLUME_CHOOSING_POLICY, VolumeChoosingPolicy.class) | ||
.newInstance(); | ||
} | ||
|
||
/** | ||
* Get a shared singleton instance of VolumeChoosingPolicy. | ||
* This method ensures that only one instance of VolumeChoosingPolicy | ||
* is created and shared across the application. | ||
* | ||
* @param conf Configuration | ||
* @return The shared VolumeChoosingPolicy instance | ||
*/ | ||
public static VolumeChoosingPolicy getSharedPolicy(ConfigurationSource conf) | ||
throws InstantiationException, IllegalAccessException { | ||
if (policyInstance == null) { | ||
synchronized (VolumeChoosingPolicyFactory.class) { | ||
if (policyInstance == null) { | ||
policyInstance = getPolicy(conf); | ||
} | ||
} | ||
} | ||
return policyInstance; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,6 +282,21 @@ public void testVolumeSetInKeyValueHandler() throws Exception { | |
DatanodeDetails datanodeDetails = mock(DatanodeDetails.class); | ||
StateContext context = ContainerTestUtils.getMockContext( | ||
datanodeDetails, conf); | ||
|
||
//Set a class which is not of sub class of VolumeChoosingPolicy | ||
conf.set(HDDS_DATANODE_VOLUME_CHOOSING_POLICY, | ||
"org.apache.hadoop.ozone.container.common.impl.HddsDispatcher"); | ||
RuntimeException exception = assertThrows(RuntimeException.class, | ||
() -> new KeyValueHandler(conf, context.getParent().getDatanodeDetails().getUuidString(), cset, volumeSet, | ||
metrics, c -> { | ||
})); | ||
|
||
assertThat(exception).hasMessageEndingWith( | ||
"class org.apache.hadoop.ozone.container.common.impl.HddsDispatcher " + | ||
"not org.apache.hadoop.ozone.container.common.interfaces.VolumeChoosingPolicy"); | ||
|
||
conf.set(HDDS_DATANODE_VOLUME_CHOOSING_POLICY, | ||
"org.apache.hadoop.ozone.container.common.volume.CapacityVolumeChoosingPolicy"); | ||
KeyValueHandler keyValueHandler = new KeyValueHandler(conf, | ||
context.getParent().getDatanodeDetails().getUuidString(), cset, | ||
volumeSet, metrics, c -> { | ||
|
@@ -297,17 +312,6 @@ public void testVolumeSetInKeyValueHandler() throws Exception { | |
metrics, c -> { }); | ||
assertEquals(ContainerLayoutVersion.FILE_PER_BLOCK, | ||
conf.getEnum(OZONE_SCM_CONTAINER_LAYOUT_KEY, ContainerLayoutVersion.FILE_PER_CHUNK)); | ||
|
||
//Set a class which is not of sub class of VolumeChoosingPolicy | ||
conf.set(HDDS_DATANODE_VOLUME_CHOOSING_POLICY, | ||
"org.apache.hadoop.ozone.container.common.impl.HddsDispatcher"); | ||
RuntimeException exception = assertThrows(RuntimeException.class, | ||
() -> new KeyValueHandler(conf, context.getParent().getDatanodeDetails().getUuidString(), cset, volumeSet, | ||
metrics, c -> { })); | ||
|
||
assertThat(exception).hasMessageEndingWith( | ||
"class org.apache.hadoop.ozone.container.common.impl.HddsDispatcher " + | ||
"not org.apache.hadoop.ozone.container.common.interfaces.VolumeChoosingPolicy"); | ||
Comment on lines
-300
to
-310
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving this to the start of the test case is not enough, the test may still fail if other test cases are run first. This is a problem with singletons. |
||
} finally { | ||
volumeSet.shutdown(); | ||
FileUtil.fullyDelete(datanodeDir); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the idea here was to share the
Random
instance among all threads. Is this change necessary, or just cleanup?