Skip to content

Commit

Permalink
add DynamicRole
Browse files Browse the repository at this point in the history
Signed-off-by: Yaliang Wu <ylwu@amazon.com>
  • Loading branch information
ylwu-amzn committed Jun 1, 2022
1 parent 2f2df16 commit 4245543
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,11 @@ public DiscoveryNode(StreamInput in) throws IOException {
}
final DiscoveryNodeRole role = roleMap.get(roleName);
if (role == null) {
roles.add(new DiscoveryNodeRole.UnknownRole(roleName, roleNameAbbreviation, canContainData));
if (in.getVersion().onOrAfter(Version.V_2_1_0)) {
roles.add(new DiscoveryNodeRole.DynamicRole(roleName, roleNameAbbreviation, canContainData));
} else {
roles.add(new DiscoveryNodeRole.UnknownRole(roleName, roleNameAbbreviation, canContainData));
}
} else {
assert roleName.equals(role.roleName()) : "role name [" + roleName + "] does not match role [" + role.roleName() + "]";
assert roleNameAbbreviation.equals(role.roleNameAbbreviation()) : "role name abbreviation ["
Expand Down Expand Up @@ -570,7 +574,7 @@ public static DiscoveryNodeRole getRoleFromRoleName(final String roleName) {
if (roleMap.containsKey(roleName)) {
return roleMap.get(roleName);
}
return new DiscoveryNodeRole.UnknownRole(roleName, roleName, false);
return new DiscoveryNodeRole.DynamicRole(roleName, roleName, false);
}

public static Set<DiscoveryNodeRole> getPossibleRoles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,19 @@ public final boolean canContainData() {

private final boolean isKnownRole;

private final boolean isDynamicRole;

/**
* Whether this role is known by this node, or is an {@link DiscoveryNodeRole.UnknownRole}.
*/
public final boolean isKnownRole() {
return isKnownRole;
}

public final boolean isDynamicRole() {
return isDynamicRole;
}

public boolean isEnabledByDefault(final Settings settings) {
return legacySetting() != null && legacySetting().get(settings);
}
Expand All @@ -110,16 +116,18 @@ protected DiscoveryNodeRole(final String roleName, final String roleNameAbbrevia
}

protected DiscoveryNodeRole(final String roleName, final String roleNameAbbreviation, final boolean canContainData) {
this(true, roleName, roleNameAbbreviation, canContainData);
this(true, false, roleName, roleNameAbbreviation, canContainData);
}

private DiscoveryNodeRole(
final boolean isKnownRole,
final boolean isDynamicRole,
final String roleName,
final String roleNameAbbreviation,
final boolean canContainData
) {
this.isKnownRole = isKnownRole;
this.isDynamicRole = isDynamicRole;
this.roleName = Objects.requireNonNull(roleName);
this.roleNameAbbreviation = Objects.requireNonNull(roleNameAbbreviation);
this.canContainData = canContainData;
Expand Down Expand Up @@ -152,12 +160,13 @@ public final boolean equals(Object o) {
return roleName.equals(that.roleName)
&& roleNameAbbreviation.equals(that.roleNameAbbreviation)
&& canContainData == that.canContainData
&& isKnownRole == that.isKnownRole;
&& isKnownRole == that.isKnownRole
&& isDynamicRole == that.isDynamicRole;
}

@Override
public final int hashCode() {
return Objects.hash(isKnownRole, roleName(), roleNameAbbreviation(), canContainData());
return Objects.hash(isKnownRole, isDynamicRole, roleName(), roleNameAbbreviation(), canContainData());
}

@Override
Expand All @@ -177,6 +186,7 @@ public final String toString() {
+ ", canContainData="
+ canContainData
+ (isKnownRole ? "" : ", isKnownRole=false")
+ (isDynamicRole ? "" : ", isDynamicRole=false")
+ '}';
}

Expand Down Expand Up @@ -298,7 +308,7 @@ public Setting<Boolean> legacySetting() {

/**
* Represents an unknown role. This can occur if a newer version adds a role that an older version does not know about, or a newer
* version removes a role that an older version knows about, or some custom role for extension function provided by plugin.
* version removes a role that an older version knows about.
*/
static class UnknownRole extends DiscoveryNodeRole {

Expand All @@ -310,7 +320,7 @@ static class UnknownRole extends DiscoveryNodeRole {
* @param canContainData whether or not nodes with the role can contain data
*/
UnknownRole(final String roleName, final String roleNameAbbreviation, final boolean canContainData) {
super(false, roleName, roleNameAbbreviation, canContainData);
super(false, false, roleName, roleNameAbbreviation, canContainData);
}

@Override
Expand All @@ -322,6 +332,32 @@ public Setting<Boolean> legacySetting() {

}

/**
* Represents a dynamic role. This can occur if a custom role that not in {@link DiscoveryNodeRole#BUILT_IN_ROLES} added for a node.
* Some plugin can support extension function with dynamic roles. For example, ML plugin may run machine learning tasks on nodes
* with "ml" dynamic role.
*/
static class DynamicRole extends DiscoveryNodeRole {

/**
* Construct a dynamic role with the specified role name and role name abbreviation.
*
* @param roleName the role name
* @param roleNameAbbreviation the role name abbreviation
* @param canContainData whether or not nodes with the role can contain data
*/
public DynamicRole(final String roleName, final String roleNameAbbreviation, final boolean canContainData) {
super(false, true, roleName, roleNameAbbreviation, canContainData);
}

@Override
public Setting<Boolean> legacySetting() {
// return null as dynamic role has no legacy setting
return null;
}

}

/**
* Check if the role is {@link #CLUSTER_MANAGER_ROLE} or {@link #MASTER_ROLE}.
* @deprecated As of 2.0, because promoting inclusive language. MASTER_ROLE is deprecated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void testDiscoveryNodeRoleEqualsHashCode() {

}

public void testUnknownRoleIsDistinctFromKnownRoles() {
public void testUnknownRoleIsDistinctFromKnownOrDynamicRoles() {
for (DiscoveryNodeRole buildInRole : DiscoveryNodeRole.BUILT_IN_ROLES) {
final DiscoveryNodeRole.UnknownRole unknownDataRole = new DiscoveryNodeRole.UnknownRole(
buildInRole.roleName(),
Expand All @@ -126,6 +126,15 @@ public void testUnknownRoleIsDistinctFromKnownRoles() {
);
assertNotEquals(buildInRole, unknownDataRole);
assertNotEquals(buildInRole.toString(), unknownDataRole.toString());
final DiscoveryNodeRole.DynamicRole dynamicRole = new DiscoveryNodeRole.DynamicRole(
buildInRole.roleName(),
buildInRole.roleNameAbbreviation(),
buildInRole.canContainData()
);
assertNotEquals(buildInRole, dynamicRole);
assertNotEquals(buildInRole.toString(), dynamicRole.toString());
assertNotEquals(unknownDataRole, dynamicRole);
assertNotEquals(unknownDataRole.toString(), dynamicRole.toString());
}
}

Expand Down

0 comments on commit 4245543

Please sign in to comment.