-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Make WorkerInfo enum (and some small refactors) #18460
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
12cb784
move WorkerState to wire package
dbw9580 e58338d
rename ACTIVE to LIVE
dbw9580 516030a
turn worker state from string to enum in WorkerInfo
dbw9580 0426385
add unrecognized worker state and set as default in worker indo
dbw9580 552687a
add copy constructors to WorkerInfo and WorkerNetAddress
dbw9580 6089227
checkstyle
dbw9580 07dffd7
fix NPE
dbw9580 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
dora/core/common/src/main/java/alluxio/master/WorkerState.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
dora/core/common/src/main/java/alluxio/wire/WorkerState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 | ||
* (the "License"). You may not use this work except in compliance with the License, which is | ||
* available at www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied, as more fully set forth in the License. | ||
* | ||
* See the NOTICE file distributed with this work for information regarding copyright ownership. | ||
*/ | ||
|
||
package alluxio.wire; | ||
|
||
/*** | ||
* The worker state maintained by master. | ||
*/ | ||
public enum WorkerState { | ||
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. anchor: moved from |
||
LIVE("LIVE"), | ||
LOST("LOST"), | ||
DECOMMISSIONED("Decommissioned"), | ||
DISABLED("Disabled"), | ||
// an unknown worker which is not recognized by the cluster membership manager, | ||
// e.g. a worker before it registers to the manager | ||
UNRECOGNIZED("UNRECOGNIZED"); | ||
|
||
private final String mState; | ||
|
||
WorkerState(String s) { | ||
mState = s; | ||
} | ||
|
||
/** | ||
* Converts from string to worker state. | ||
* | ||
* @param workerState string representation of worker state | ||
* @return worker state | ||
* @throws IllegalArgumentException if the state is unknown | ||
*/ | ||
public static WorkerState of(String workerState) throws IllegalArgumentException { | ||
switch (workerState) { | ||
case "LIVE": | ||
return LIVE; | ||
case "LOST": | ||
return LOST; | ||
case "Decommissioned": | ||
return DECOMMISSIONED; | ||
case "Disabled": | ||
return DISABLED; | ||
case "UNRECOGNIZED": | ||
return UNRECOGNIZED; | ||
default: | ||
throw new IllegalArgumentException("Unknown worker state: " + workerState); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return mState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,21 @@ | |
|
||
package alluxio.wire; | ||
|
||
import static org.junit.Assert.assertNotEquals; | ||
|
||
import alluxio.Constants; | ||
import alluxio.grpc.GrpcUtils; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.base.Defaults; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.commons.codec.digest.DigestUtils; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Random; | ||
|
@@ -51,6 +57,39 @@ public void lastContactSecComparator() { | |
Assert.assertTrue(compareLostWorkersWithTimes(1, -1) > 0); | ||
} | ||
|
||
@Test | ||
public void copyConstructor() throws IllegalAccessException { | ||
WorkerInfo original = new WorkerInfo() | ||
.setId(1) | ||
.setIdentity(WorkerIdentityTestUtils.ofLegacyId(1)) | ||
.setAddress(new WorkerNetAddress().setHost("host1")) | ||
.setBlockCount(1) | ||
.setCapacityBytes(1) | ||
.setUsedBytes(1) | ||
.setCapacityBytesOnTiers(ImmutableMap.of()) | ||
.setUsedBytesOnTiers(ImmutableMap.of()) | ||
.setLastContactSec(1) | ||
.setStartTimeMs(1) | ||
.setState(WorkerState.LIVE) | ||
.setRevision("rev1") | ||
.setVersion("ver1"); | ||
WorkerInfo copied = new WorkerInfo(original); | ||
// copied instance should contain exactly the same content | ||
checkEquality(original, copied); | ||
// mutate any non-final field in the copy, | ||
// and the change should not be reflected in the original | ||
for (Field field : WorkerInfo.class.getDeclaredFields()) { | ||
int fieldModifiers = field.getModifiers(); | ||
if (Modifier.isStatic(fieldModifiers) || Modifier.isFinal(fieldModifiers)) { | ||
continue; | ||
} | ||
field.setAccessible(true); | ||
// set fields in the copy to their default value | ||
field.set(copied, Defaults.defaultValue(field.getType())); | ||
Comment on lines
+83
to
+88
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. lol, works for me |
||
assertNotEquals(field.getName(), field.get(original), field.get(copied)); | ||
} | ||
} | ||
|
||
public void checkEquality(WorkerInfo a, WorkerInfo b) { | ||
Assert.assertEquals(a.getId(), b.getId()); | ||
Assert.assertEquals(a.getAddress(), b.getAddress()); | ||
|
@@ -88,7 +127,7 @@ public static WorkerInfo createRandom() { | |
capacityBytesOnTiers.put(Constants.MEDIUM_MEM, capacityBytes); | ||
Map<String, Long> usedBytesOnTiers = new HashMap<>(); | ||
usedBytesOnTiers.put(Constants.MEDIUM_MEM, usedBytes); | ||
String state = random.nextInt(2) == 1 ? "In Service" : "Out of Service"; | ||
WorkerState state = random.nextInt(2) == 1 ? WorkerState.LIVE : WorkerState.LOST; | ||
String version = String.format("%d.%d.%d", random.nextInt(10), | ||
random.nextInt(20), random.nextInt(10)); | ||
String revision = DigestUtils.sha1Hex(RandomStringUtils.random(10)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 had to assign a default value for worker state. A
UNRECOGNIZED
state means either the code that creates theWorkerInfo
object did not set its state explicitly, or the worker is unknown to the membership service (e.g. never registered).