-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
FileResourceGroupConfigurationManager.java
126 lines (112 loc) · 4.47 KB
/
FileResourceGroupConfigurationManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.resourceGroups;
import com.facebook.presto.spi.memory.ClusterMemoryPoolManager;
import com.facebook.presto.spi.resourceGroups.SelectionCriteria;
import com.facebook.presto.spi.resourceGroups.ResourceGroup;
import com.facebook.presto.spi.resourceGroups.ResourceGroupId;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.google.common.annotations.VisibleForTesting;
import io.airlift.json.JsonCodec;
import io.airlift.json.JsonCodecFactory;
import io.airlift.json.ObjectMapperProvider;
import io.airlift.units.Duration;
import javax.inject.Inject;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public class FileResourceGroupConfigurationManager
extends AbstractResourceConfigurationManager
{
private static final JsonCodec<ManagerSpec> CODEC = new JsonCodecFactory(
() -> new ObjectMapperProvider().get().enable(FAIL_ON_UNKNOWN_PROPERTIES))
.jsonCodec(ManagerSpec.class);
private final List<ResourceGroupSpec> rootGroups;
private final List<ResourceGroupSelector> selectors;
private final Optional<Duration> cpuQuotaPeriod;
@Inject
public FileResourceGroupConfigurationManager(ClusterMemoryPoolManager memoryPoolManager, FileResourceGroupConfig config)
{
super(memoryPoolManager);
requireNonNull(config, "config is null");
ManagerSpec managerSpec;
try {
managerSpec = CODEC.fromJson(Files.readAllBytes(Paths.get(config.getConfigFile())));
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
catch (IllegalArgumentException e) {
Throwable cause = e.getCause();
if (cause instanceof UnrecognizedPropertyException) {
UnrecognizedPropertyException ex = (UnrecognizedPropertyException) cause;
String message = format("Unknown property at line %s:%s: %s",
ex.getLocation().getLineNr(),
ex.getLocation().getColumnNr(),
ex.getPropertyName());
throw new IllegalArgumentException(message, e);
}
if (cause instanceof JsonMappingException) {
// remove the extra "through reference chain" message
if (cause.getCause() != null) {
cause = cause.getCause();
}
throw new IllegalArgumentException(cause.getMessage(), e);
}
throw e;
}
this.rootGroups = managerSpec.getRootGroups();
this.cpuQuotaPeriod = managerSpec.getCpuQuotaPeriod();
validateRootGroups(managerSpec);
this.selectors = buildSelectors(managerSpec);
}
@Override
protected Optional<Duration> getCpuQuotaPeriod()
{
return cpuQuotaPeriod;
}
@Override
protected List<ResourceGroupSpec> getRootGroups()
{
return rootGroups;
}
@Override
public void configure(ResourceGroup group, SelectionCriteria criteria)
{
Map.Entry<ResourceGroupIdTemplate, ResourceGroupSpec> entry = getMatchingSpec(group, criteria);
configureGroup(group, entry.getValue());
}
@Override
public Optional<ResourceGroupId> match(SelectionCriteria criteria)
{
return selectors.stream()
.map(s -> s.match(criteria))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
}
@VisibleForTesting
public List<ResourceGroupSelector> getSelectors()
{
return selectors;
}
}