66
77import com .github .rholder .retry .RetryerBuilder ;
88import com .google .common .collect .ImmutableMap ;
9- import com .google .protobuf .util .JsonFormat ;
109import com .typesafe .config .Config ;
11- import com .typesafe .config .ConfigObject ;
1210import io .grpc .Channel ;
1311import io .grpc .Status ;
1412import io .grpc .StatusRuntimeException ;
2321import java .util .concurrent .TimeUnit ;
2422import java .util .function .Function ;
2523import java .util .stream .Collectors ;
26- import lombok .SneakyThrows ;
2724import lombok .extern .slf4j .Slf4j ;
2825import org .hypertrace .config .objectstore .ContextualConfigObject ;
2926import org .hypertrace .config .service .change .event .api .ConfigChangeEventGenerator ;
@@ -51,9 +48,7 @@ public class LabelsConfigServiceImpl extends LabelsConfigServiceGrpc.LabelsConfi
5148 private static final String LABELS_CONFIG_SERVICE_CONFIG = "labels.config.service" ;
5249 private static final String SYSTEM_LABELS = "system.labels" ;
5350 private final LabelStore labelStore ;
54- private final List <Label > systemLabels ;
55- private final Map <String , Label > systemLabelsIdLabelMap ;
56- private final Map <String , Label > systemLabelsKeyLabelMap ;
51+ private final SystemLabelInfoUtils systemLabelInfoUtils ;
5752
5853 public LabelsConfigServiceImpl (
5954 Channel configChannel , Config config , ConfigChangeEventGenerator configChangeEventGenerator ) {
@@ -64,39 +59,7 @@ public LabelsConfigServiceImpl(
6459 RequestContextClientCallCredsProviderFactory .getClientCallCredsProvider ()
6560 .get ()),
6661 configChangeEventGenerator );
67- List <? extends ConfigObject > systemLabelsObjectList = null ;
68- if (config .hasPath (LABELS_CONFIG_SERVICE_CONFIG )) {
69- Config labelConfig = config .getConfig (LABELS_CONFIG_SERVICE_CONFIG );
70- if (labelConfig .hasPath (SYSTEM_LABELS )) {
71- systemLabelsObjectList = labelConfig .getObjectList (SYSTEM_LABELS );
72- }
73- }
74- if (systemLabelsObjectList != null ) {
75- systemLabels = buildSystemLabelList (systemLabelsObjectList );
76- systemLabelsIdLabelMap =
77- systemLabels .stream ().collect (Collectors .toUnmodifiableMap (Label ::getId , identity ()));
78- systemLabelsKeyLabelMap =
79- systemLabels .stream ()
80- .collect (Collectors .toUnmodifiableMap (label -> label .getData ().getKey (), identity ()));
81- } else {
82- systemLabels = Collections .emptyList ();
83- systemLabelsIdLabelMap = Collections .emptyMap ();
84- systemLabelsKeyLabelMap = Collections .emptyMap ();
85- }
86- }
87-
88- private List <Label > buildSystemLabelList (List <? extends ConfigObject > configObjectList ) {
89- return configObjectList .stream ()
90- .map (LabelsConfigServiceImpl ::buildLabelFromConfig )
91- .collect (Collectors .toUnmodifiableList ());
92- }
93-
94- @ SneakyThrows
95- private static Label buildLabelFromConfig (ConfigObject configObject ) {
96- String jsonString = configObject .render ();
97- Label .Builder builder = Label .newBuilder ();
98- JsonFormat .parser ().merge (jsonString , builder );
99- return builder .build ();
62+ systemLabelInfoUtils = new SystemLabelInfoUtils (config , labelStore );
10063 }
10164
10265 @ Override
@@ -200,7 +163,8 @@ public void getLabel(GetLabelRequest request, StreamObserver<GetLabelResponse> r
200163 .getData (requestContext , labelId )
201164 .orElseGet (
202165 () ->
203- Optional .ofNullable (systemLabelsIdLabelMap .get (labelId ))
166+ Optional .ofNullable (
167+ systemLabelInfoUtils .getSystemLabelsIdLabelMap ().get (labelId ))
204168 .orElseThrow (Status .NOT_FOUND ::asRuntimeException ));
205169 responseObserver .onNext (GetLabelResponse .newBuilder ().setLabel (label ).build ());
206170 responseObserver .onCompleted ();
@@ -221,7 +185,7 @@ public void getLabels(
221185 tenantLabels .stream ()
222186 .collect (Collectors .toUnmodifiableMap (Label ::getId , Function .identity ()));
223187 List <Label > allLabels = new ArrayList <>(tenantLabels );
224- systemLabels .stream ()
188+ systemLabelInfoUtils . getSystemLabels () .stream ()
225189 .filter (label -> !tenantLabelsMap .containsKey (label .getId ()))
226190 .forEach (allLabels ::add );
227191 responseObserver .onNext (GetLabelsResponse .newBuilder ().addAllLabels (allLabels ).build ());
@@ -244,10 +208,12 @@ public void updateLabel(
244208 .getData (requestContext , request .getId ())
245209 .orElseGet (
246210 () ->
247- Optional .ofNullable (systemLabelsIdLabelMap .get (request .getId ()))
211+ Optional .ofNullable (
212+ systemLabelInfoUtils .getSystemLabelsIdLabelMap ().get (request .getId ()))
248213 .orElseThrow (Status .NOT_FOUND ::asRuntimeException ));
249214 Label updateLabel = oldLabel .toBuilder ().setData (updateLabelData ).build ();
250215 Label updateLabelInRes = labelStore .upsertObject (requestContext , updateLabel ).getData ();
216+ systemLabelInfoUtils .updateSystemLabelInfo (updateLabelInRes );
251217 responseObserver .onNext (UpdateLabelResponse .newBuilder ().setLabel (updateLabelInRes ).build ());
252218 responseObserver .onCompleted ();
253219 } catch (Exception e ) {
@@ -261,7 +227,7 @@ public void deleteLabel(
261227 try {
262228 RequestContext requestContext = RequestContext .CURRENT .get ();
263229 String labelId = request .getId ();
264- if (systemLabelsIdLabelMap .containsKey (labelId )) {
230+ if (systemLabelInfoUtils . getSystemLabelsIdLabelMap () .containsKey (labelId )) {
265231 // Deleting a system label
266232 responseObserver .onError (new StatusRuntimeException (Status .INVALID_ARGUMENT ));
267233 return ;
@@ -287,7 +253,8 @@ private boolean isDuplicateKey(RequestContext requestContext, String key) {
287253 }
288254
289255 private Map <String , Label > getLabelsMap (RequestContext requestContext ) {
290- Map <String , Label > existingLabelsMap = new HashMap <>(systemLabelsKeyLabelMap );
256+ Map <String , Label > existingLabelsMap =
257+ new HashMap <>(systemLabelInfoUtils .getSystemLabelsKeyLabelMap ());
291258 labelStore .getAllObjects (requestContext ).stream ()
292259 .map (ContextualConfigObject ::getData )
293260 .forEach (label -> existingLabelsMap .put (label .getData ().getKey (), label ));
0 commit comments