Skip to content

Commit bde8ff8

Browse files
authored
Fix IsInstanceEnabled for backward compatibility (apache#2972)
Fix IsInstanceEnabled for backward compatibility
1 parent 522554e commit bde8ff8

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

helix-core/src/main/java/org/apache/helix/model/InstanceConfig.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,8 @@ public InstanceOperation getInstanceOperation() {
642642
.build();
643643
}
644644

645-
// Always respect the HELIX_ENABLED being set to false when instance operation is unset
646-
// for backwards compatibility.
645+
// if instance operation is not DISABLED, we always respect the HELIX_ENABLED being set to false
646+
// when instance operation is unset for backwards compatibility.
647647
if (!_record.getBooleanField(InstanceConfigProperty.HELIX_ENABLED.name(),
648648
HELIX_ENABLED_DEFAULT_VALUE)
649649
&& (InstanceConstants.INSTANCE_DISABLED_OVERRIDABLE_OPERATIONS.contains(
@@ -656,6 +656,17 @@ public InstanceOperation getInstanceOperation() {
656656
.build();
657657
}
658658

659+
// if instance operation is DISABLE, we override it to ENABLE if HELIX_ENABLED set to true
660+
if (activeInstanceOperation.getOperation() == InstanceConstants.InstanceOperation.DISABLE) {
661+
// it is not likely that HELIX_ENABLED is unset, because when we set operation to disable,
662+
// we always set HELIX_ENABLED to false
663+
// If instance is enabled by old version helix (not having instance operation), the instance config
664+
// will have HELIX_ENABLED set to true. In this case, we should override the instance operation to ENABLE
665+
if ("true".equals(_record.getSimpleField(InstanceConfigProperty.HELIX_ENABLED.name()))) {
666+
return new InstanceOperation.Builder().setOperation(InstanceConstants.InstanceOperation.ENABLE).build();
667+
}
668+
}
669+
659670
return activeInstanceOperation;
660671
}
661672

helix-core/src/test/java/org/apache/helix/model/TestInstanceConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ public void testSetInstanceEnableWithReason() {
7474
InstanceConstants.InstanceDisabledType.USER_OPERATION.toString());
7575
}
7676

77+
@Test
78+
public void testEnabledInstanceBackwardCompatibility() {
79+
// if instance is disabled by instanceOperation, and enabled by HELIX_ENABLED, the instance should be enabled
80+
InstanceConfig instanceConfig =
81+
new InstanceConfig.Builder().setInstanceOperation(InstanceConstants.InstanceOperation.DISABLE).build("id");
82+
Assert.assertFalse(instanceConfig.getInstanceEnabled());
83+
// assume an old version client enabled the instance by setting HELIX_ENABLED to true
84+
instanceConfig.getRecord().setSimpleField(InstanceConfig.InstanceConfigProperty.HELIX_ENABLED.name(), "true");
85+
Assert.assertTrue(instanceConfig.getInstanceEnabled());
86+
instanceConfig.setInstanceOperation(InstanceConstants.InstanceOperation.ENABLE);
87+
88+
// disable the instance by setting HELIX_ENABLED to false
89+
instanceConfig.getRecord().setSimpleField(InstanceConfig.InstanceConfigProperty.HELIX_ENABLED.name(), "false");
90+
Assert.assertFalse(instanceConfig.getInstanceEnabled());
91+
}
92+
7793
@Test
7894
public void testGetParsedDomainEmptyDomain() {
7995
InstanceConfig instanceConfig = new InstanceConfig(new ZNRecord("id"));

0 commit comments

Comments
 (0)