Skip to content

Commit 6ff29bf

Browse files
spotless Apply
Signed-off-by: Kiran Prakash <[email protected]>
1 parent eb22efb commit 6ff29bf

18 files changed

+389
-391
lines changed

server/src/main/java/org/opensearch/cluster/metadata/Metadata.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ public boolean isSegmentReplicationEnabled(String indexName) {
126126
.orElse(false);
127127
}
128128

129-
public Map<String, Sandbox> sandboxes() {
130-
// stub
131-
return Collections.emptyMap();
132-
}
129+
public Map<String, Sandbox> sandboxes() {
130+
// stub
131+
return Collections.emptyMap();
132+
}
133133

134-
/**
134+
/**
135135
* Context of the XContent.
136136
*
137137
* @opensearch.api

server/src/main/java/org/opensearch/cluster/metadata/Sandbox.java

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,72 +16,72 @@
1616

1717
@ExperimentalApi
1818
public class Sandbox {
19-
//TODO Kaushal should have implemented hashcode and equals
20-
private SandboxMode mode;
19+
// TODO Kaushal should have implemented hashcode and equals
20+
private SandboxMode mode;
2121

22-
public SandboxMode getMode() {
23-
return mode;
24-
}
25-
26-
public ResourceLimit getResourceLimitFor(SandboxResourceType resourceType) {
27-
return null;
28-
}
29-
30-
public String getName() {
31-
return "";
32-
}
33-
34-
public String getId() {
35-
return "";
36-
}
22+
public SandboxMode getMode() {
23+
return mode;
24+
}
3725

38-
public List<ResourceLimit> getResourceLimits() {
39-
return Collections.emptyList();
40-
}
26+
public ResourceLimit getResourceLimitFor(SandboxResourceType resourceType) {
27+
return null;
28+
}
4129

42-
@ExperimentalApi
43-
public class ResourceLimit {
44-
public Long getThresholdInLong() {
45-
return 0L;
30+
public String getName() {
31+
return "";
4632
}
4733

48-
public SandboxResourceType getResourceType() {
49-
return null;
34+
public String getId() {
35+
return "";
5036
}
5137

52-
public Long getThreshold() {
53-
return 0L;
38+
public List<ResourceLimit> getResourceLimits() {
39+
return Collections.emptyList();
5440
}
55-
}
5641

57-
@ExperimentalApi
58-
public enum SandboxMode {
59-
SOFT("soft"),
60-
ENFORCED("enforced"),
61-
MONITOR("monitor");
42+
@ExperimentalApi
43+
public class ResourceLimit {
44+
public Long getThresholdInLong() {
45+
return 0L;
46+
}
6247

63-
private final String name;
48+
public SandboxResourceType getResourceType() {
49+
return null;
50+
}
6451

65-
SandboxMode(String mode) {
66-
this.name = mode;
52+
public Long getThreshold() {
53+
return 0L;
54+
}
6755
}
6856

69-
public String getName() {
70-
return name;
71-
}
57+
@ExperimentalApi
58+
public enum SandboxMode {
59+
SOFT("soft"),
60+
ENFORCED("enforced"),
61+
MONITOR("monitor");
62+
63+
private final String name;
64+
65+
SandboxMode(String mode) {
66+
this.name = mode;
67+
}
68+
69+
public String getName() {
70+
return name;
71+
}
72+
73+
public static SandboxMode fromName(String s) {
74+
switch (s) {
75+
case "soft":
76+
return SOFT;
77+
case "enforced":
78+
return ENFORCED;
79+
case "monitor":
80+
return MONITOR;
81+
default:
82+
throw new IllegalArgumentException("Invalid value for SandboxMode: " + s);
83+
}
84+
}
7285

73-
public static SandboxMode fromName(String s) {
74-
switch (s) {
75-
case "soft":
76-
return SOFT;
77-
case "enforced":
78-
return ENFORCED;
79-
case "monitor":
80-
return MONITOR;
81-
default:
82-
throw new IllegalArgumentException("Invalid value for SandboxMode: " + s);
83-
}
8486
}
85-
86-
}
8787
}

server/src/main/java/org/opensearch/search/sandboxing/SandboxLevelResourceUsageView.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,40 @@
2121
@ExperimentalApi
2222
public class SandboxLevelResourceUsageView {
2323

24-
private final String sandboxId;
25-
private final Map<SandboxResourceType, Long> resourceUsage;
26-
private final List<Task> activeTasks;
27-
28-
public SandboxLevelResourceUsageView(String sandboxId) {
29-
this.sandboxId = sandboxId;
30-
this.resourceUsage = new HashMap<>();
31-
this.activeTasks = new ArrayList<>();
32-
}
33-
34-
public SandboxLevelResourceUsageView(String sandboxId, Map<SandboxResourceType, Long> resourceUsage, List<Task> activeTasks) {
35-
this.sandboxId = sandboxId;
36-
this.resourceUsage = resourceUsage;
37-
this.activeTasks = activeTasks;
38-
}
39-
40-
public Map<SandboxResourceType, Long> getResourceUsageData() {
41-
return resourceUsage;
42-
}
43-
44-
public List<Task> getActiveTasks() {
45-
return activeTasks;
46-
}
47-
48-
@Override
49-
public boolean equals(Object o) {
50-
if (this == o) return true;
51-
if (o == null || getClass() != o.getClass()) return false;
52-
SandboxLevelResourceUsageView that = (SandboxLevelResourceUsageView) o;
53-
return Objects.equals(sandboxId, that.sandboxId);
54-
}
55-
56-
@Override
57-
public int hashCode() {
58-
return Objects.hashCode(sandboxId);
59-
}
24+
private final String sandboxId;
25+
private final Map<SandboxResourceType, Long> resourceUsage;
26+
private final List<Task> activeTasks;
27+
28+
public SandboxLevelResourceUsageView(String sandboxId) {
29+
this.sandboxId = sandboxId;
30+
this.resourceUsage = new HashMap<>();
31+
this.activeTasks = new ArrayList<>();
32+
}
33+
34+
public SandboxLevelResourceUsageView(String sandboxId, Map<SandboxResourceType, Long> resourceUsage, List<Task> activeTasks) {
35+
this.sandboxId = sandboxId;
36+
this.resourceUsage = resourceUsage;
37+
this.activeTasks = activeTasks;
38+
}
39+
40+
public Map<SandboxResourceType, Long> getResourceUsageData() {
41+
return resourceUsage;
42+
}
43+
44+
public List<Task> getActiveTasks() {
45+
return activeTasks;
46+
}
47+
48+
@Override
49+
public boolean equals(Object o) {
50+
if (this == o) return true;
51+
if (o == null || getClass() != o.getClass()) return false;
52+
SandboxLevelResourceUsageView that = (SandboxLevelResourceUsageView) o;
53+
return Objects.equals(sandboxId, that.sandboxId);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hashCode(sandboxId);
59+
}
6060
}

server/src/main/java/org/opensearch/search/sandboxing/cancellation/AbstractTaskCancellation.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@ private boolean shouldCancelTasks(Sandbox sandbox, SandboxResourceType resourceT
9393

9494
private List<TaskCancellation> getTaskCancellations(Sandbox sandbox, SandboxResourceType resourceType) {
9595
return taskSelectionStrategy.selectTasksForCancellation(
96-
getAllTasksInSandbox(sandbox.getId()),
97-
getReduceBy(sandbox, resourceType),
98-
resourceType)
99-
.stream()
100-
.map(task -> createTaskCancellation((CancellableTask) task))
101-
.collect(Collectors.toList());
96+
getAllTasksInSandbox(sandbox.getId()),
97+
getReduceBy(sandbox, resourceType),
98+
resourceType
99+
).stream().map(task -> createTaskCancellation((CancellableTask) task)).collect(Collectors.toList());
102100
}
103101

104102
private long getReduceBy(Sandbox sandbox, SandboxResourceType resourceType) {

server/src/main/java/org/opensearch/search/sandboxing/cancellation/AbstractTaskSelectionStrategy.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,29 @@
1919

2020
public abstract class AbstractTaskSelectionStrategy implements TaskSelectionStrategy {
2121

22-
public abstract Comparator<Task> sortingCondition();
23-
24-
@Override
25-
public List<Task> selectTasksForCancellation(List<Task> tasks, long limit, SandboxResourceType resourceType) {
26-
if (limit < 0) {
27-
throw new IllegalArgumentException("reduceBy has to be greater than zero");
28-
}
29-
if(limit == 0) {
30-
return Collections.emptyList();
31-
}
32-
33-
List<Task> sortedTasks = tasks.stream()
34-
.sorted(sortingCondition())
35-
.collect(Collectors.toList());
36-
37-
List<Task> selectedTasks = new ArrayList<>();
38-
long accumulated = 0;
39-
40-
for (Task task : sortedTasks) {
41-
selectedTasks.add(task);
42-
accumulated += resourceType.getResourceUsage(task);
43-
if (accumulated >= limit) {
44-
break;
45-
}
22+
public abstract Comparator<Task> sortingCondition();
23+
24+
@Override
25+
public List<Task> selectTasksForCancellation(List<Task> tasks, long limit, SandboxResourceType resourceType) {
26+
if (limit < 0) {
27+
throw new IllegalArgumentException("reduceBy has to be greater than zero");
28+
}
29+
if (limit == 0) {
30+
return Collections.emptyList();
31+
}
32+
33+
List<Task> sortedTasks = tasks.stream().sorted(sortingCondition()).collect(Collectors.toList());
34+
35+
List<Task> selectedTasks = new ArrayList<>();
36+
long accumulated = 0;
37+
38+
for (Task task : sortedTasks) {
39+
selectedTasks.add(task);
40+
accumulated += resourceType.getResourceUsage(task);
41+
if (accumulated >= limit) {
42+
break;
43+
}
44+
}
45+
return selectedTasks;
4646
}
47-
return selectedTasks;
48-
}
4947
}

server/src/main/java/org/opensearch/search/sandboxing/cancellation/DefaultTaskCancellation.java

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,55 @@
99
package org.opensearch.search.sandboxing.cancellation;
1010

1111
import org.opensearch.cluster.metadata.Sandbox;
12-
import org.opensearch.search.sandboxing.resourcetype.SandboxResourceType;
1312
import org.opensearch.search.sandboxing.SandboxLevelResourceUsageView;
13+
import org.opensearch.search.sandboxing.resourcetype.SandboxResourceType;
1414

1515
import java.util.ArrayList;
1616
import java.util.List;
1717
import java.util.Map;
1818
import java.util.Set;
1919

2020
public class DefaultTaskCancellation extends AbstractTaskCancellation {
21-
public DefaultTaskCancellation(
22-
TaskSelectionStrategy cancellationStrategy,
23-
Map<String, SandboxLevelResourceUsageView> sandboxLevelViews,
24-
Set<Sandbox> activeSandboxes
25-
) {
26-
super(cancellationStrategy, sandboxLevelViews, activeSandboxes);
27-
}
28-
29-
/**
30-
* // TODO
31-
* This should cover 3 scenarios
32-
* - if node not in duress
33-
* - pick sandboxes in enforced mode only
34-
* - if node in duress
35-
* - pick sandboxes in enforced mode
36-
* - tasks running in deleted sandboxes with tasks running
37-
* - pick sandboxes in enforced mode
38-
*/
39-
public List<Sandbox> getSandboxesToCancelFrom() {
40-
final List<Sandbox> sandboxesToCancelFrom = new ArrayList<>();
41-
42-
for (Sandbox sandbox : this.activeSandboxes) {
43-
Map<SandboxResourceType, Long> currentResourceUsage = getResourceUsage(sandbox.getId());
44-
45-
for (Sandbox.ResourceLimit resourceLimit : sandbox.getResourceLimits()) {
46-
if (isBreachingThreshold(currentResourceUsage, resourceLimit)) {
47-
sandboxesToCancelFrom.add(sandbox);
48-
break;
49-
}
50-
}
21+
public DefaultTaskCancellation(
22+
TaskSelectionStrategy cancellationStrategy,
23+
Map<String, SandboxLevelResourceUsageView> sandboxLevelViews,
24+
Set<Sandbox> activeSandboxes
25+
) {
26+
super(cancellationStrategy, sandboxLevelViews, activeSandboxes);
5127
}
5228

53-
return sandboxesToCancelFrom;
54-
}
29+
/**
30+
* // TODO
31+
* This should cover 3 scenarios
32+
* - if node not in duress
33+
* - pick sandboxes in enforced mode only
34+
* - if node in duress
35+
* - pick sandboxes in enforced mode
36+
* - tasks running in deleted sandboxes with tasks running
37+
* - pick sandboxes in enforced mode
38+
*/
39+
public List<Sandbox> getSandboxesToCancelFrom() {
40+
final List<Sandbox> sandboxesToCancelFrom = new ArrayList<>();
41+
42+
for (Sandbox sandbox : this.activeSandboxes) {
43+
Map<SandboxResourceType, Long> currentResourceUsage = getResourceUsage(sandbox.getId());
44+
45+
for (Sandbox.ResourceLimit resourceLimit : sandbox.getResourceLimits()) {
46+
if (isBreachingThreshold(currentResourceUsage, resourceLimit)) {
47+
sandboxesToCancelFrom.add(sandbox);
48+
break;
49+
}
50+
}
51+
}
52+
53+
return sandboxesToCancelFrom;
54+
}
5555

56-
private boolean isBreachingThreshold(Map<SandboxResourceType, Long> currentResourceUsage, Sandbox.ResourceLimit resourceLimit) {
57-
return currentResourceUsage.get(resourceLimit.getResourceType()) > resourceLimit.getThreshold();
58-
}
56+
private boolean isBreachingThreshold(Map<SandboxResourceType, Long> currentResourceUsage, Sandbox.ResourceLimit resourceLimit) {
57+
return currentResourceUsage.get(resourceLimit.getResourceType()) > resourceLimit.getThreshold();
58+
}
5959

60-
private Map<SandboxResourceType, Long> getResourceUsage(String sandboxId) {
61-
return sandboxLevelViews.get(sandboxId).getResourceUsageData();
62-
}
60+
private Map<SandboxResourceType, Long> getResourceUsage(String sandboxId) {
61+
return sandboxLevelViews.get(sandboxId).getResourceUsageData();
62+
}
6363
}

0 commit comments

Comments
 (0)