1818
1919package org .apache .skywalking .oap .server .storage .plugin .banyandb ;
2020
21- import com .fasterxml .jackson .databind .ObjectMapper ;
2221import com .google .gson .JsonObject ;
23- import io .grpc .Status ;
2422import lombok .Builder ;
2523import lombok .Data ;
2624import lombok .EqualsAndHashCode ;
2725import lombok .Getter ;
28- import lombok .NoArgsConstructor ;
2926import lombok .RequiredArgsConstructor ;
30- import lombok .Setter ;
3127import lombok .Singular ;
3228import lombok .ToString ;
3329import lombok .extern .slf4j .Slf4j ;
3430import org .apache .skywalking .banyandb .common .v1 .BanyandbCommon ;
35- import org .apache .skywalking .banyandb .common .v1 .BanyandbCommon .Catalog ;
36- import org .apache .skywalking .banyandb .common .v1 .BanyandbCommon .Group ;
37- import org .apache .skywalking .banyandb .common .v1 .BanyandbCommon .IntervalRule ;
3831import org .apache .skywalking .banyandb .common .v1 .BanyandbCommon .Metadata ;
39- import org .apache .skywalking .banyandb .common .v1 .BanyandbCommon .ResourceOpts ;
4032import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase ;
4133import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase .CompressionMethod ;
4234import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase .EncodingMethod ;
5042import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase .TagType ;
5143import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase .TopNAggregation ;
5244import org .apache .skywalking .banyandb .model .v1 .BanyandbModel ;
53- import org .apache .skywalking .banyandb .v1 .client .BanyanDBClient ;
54- import org .apache .skywalking .banyandb .v1 .client .grpc .exception .BanyanDBException ;
5545import org .apache .skywalking .banyandb .v1 .client .metadata .Duration ;
56- import org .apache .skywalking .banyandb .v1 .client .metadata .MetadataCache ;
57- import org .apache .skywalking .banyandb .v1 .client .metadata .ResourceExist ;
5846import org .apache .skywalking .oap .server .core .analysis .DownSampling ;
5947import org .apache .skywalking .oap .server .core .analysis .metrics .IntList ;
6048import org .apache .skywalking .oap .server .core .analysis .metrics .Metrics ;
7765import java .util .ArrayList ;
7866import java .util .Comparator ;
7967import java .util .HashMap ;
80- import java .util .HashSet ;
8168import java .util .List ;
8269import java .util .Map ;
8370import java .util .Objects ;
9077public enum MetadataRegistry {
9178 INSTANCE ;
9279
93- private static final ObjectMapper MAPPER = new ObjectMapper ();
94- // BanyanDB group setting aligned with the OAP settings
95- private static final Set <String > GROUP_ALIGNED = new HashSet <>();
9680 private final Map <String , Schema > registry = new HashMap <>();
9781
9882 public StreamModel registerStreamModel (Model model , BanyanDBStorageConfig config , DownSamplingConfigService configService ) {
@@ -187,13 +171,13 @@ public MeasureModel registerMeasureModel(Model model, BanyanDBStorageConfig conf
187171 schemaBuilder .field (field .getName ());
188172 }
189173 // parse TopN
190- schemaBuilder .topNSpec (parseTopNSpec (model , schemaMetadata .name ()));
174+ schemaBuilder .topNSpec (parseTopNSpec (model , schemaMetadata .group , schemaMetadata . name ()));
191175
192176 registry .put (schemaMetadata .name (), schemaBuilder .build ());
193177 return new MeasureModel (builder .build (), indexRules );
194178 }
195179
196- private TopNSpec parseTopNSpec (final Model model , final String measureName )
180+ private TopNAggregation parseTopNSpec (final Model model , final String group , final String measureName )
197181 throws StorageException {
198182 if (model .getBanyanDBModelExtension ().getTopN () == null ) {
199183 return null ;
@@ -208,14 +192,16 @@ private TopNSpec parseTopNSpec(final Model model, final String measureName)
208192 if (CollectionUtils .isEmpty (model .getBanyanDBModelExtension ().getTopN ().getGroupByTagNames ())) {
209193 throw new StorageException ("invalid groupBy tags: " + model .getBanyanDBModelExtension ().getTopN ().getGroupByTagNames ());
210194 }
211- return TopNSpec .builder ()
212- .name (measureName + "_topn" )
213- .lruSize (model .getBanyanDBModelExtension ().getTopN ().getLruSize ())
214- .countersNumber (model .getBanyanDBModelExtension ().getTopN ().getCountersNumber ())
215- .fieldName (valueColumnOpt .get ().getValueCName ())
216- .groupByTagNames (model .getBanyanDBModelExtension ().getTopN ().getGroupByTagNames ())
217- .sort (BanyandbModel .Sort .SORT_UNSPECIFIED ) // include both TopN and BottomN
218- .build ();
195+ return TopNAggregation .newBuilder ()
196+ .setMetadata (
197+ Metadata .newBuilder ().setGroup (group ).setName (Schema .formatTopNName (measureName )))
198+ .setSourceMeasure (Metadata .newBuilder ().setGroup (group ).setName (measureName ))
199+ .setFieldValueSort (BanyandbModel .Sort .SORT_UNSPECIFIED ) // include both TopN and BottomN
200+ .setFieldName (valueColumnOpt .get ().getValueCName ())
201+ .addAllGroupByTagNames (model .getBanyanDBModelExtension ().getTopN ().getGroupByTagNames ())
202+ .setCountersNumber (model .getBanyanDBModelExtension ().getTopN ().getCountersNumber ())
203+ .setLruSize (model .getBanyanDBModelExtension ().getTopN ().getLruSize ())
204+ .build ();
219205 }
220206
221207 public Schema findMetadata (final Model model ) {
@@ -306,8 +292,8 @@ Duration downSamplingDuration(DownSampling downSampling) {
306292
307293 IndexRule indexRule (String group , String tagName , BanyanDB .MatchQuery .AnalyzerType analyzer ) {
308294 IndexRule .Builder builder = IndexRule .newBuilder ()
309- .setMetadata (Metadata .newBuilder ().setName (tagName ).setGroup (group ))
310- .setType (IndexRule .Type .TYPE_INVERTED ).addTags (tagName );
295+ .setMetadata (Metadata .newBuilder ().setName (tagName ).setGroup (group ))
296+ .setType (IndexRule .Type .TYPE_INVERTED ).addTags (tagName );
311297 if (analyzer != null ) {
312298 switch (analyzer ) {
313299 case KEYWORD :
@@ -553,36 +539,6 @@ static String formatName(String modelName, DownSampling downSampling) {
553539 return modelName + "_" + downSampling .getName ();
554540 }
555541
556- public Optional <Object > findRemoteSchema (BanyanDBClient client ) throws BanyanDBException {
557- try {
558- switch (kind ) {
559- case STREAM :
560- return Optional .ofNullable (client .findStream (this .group , this .name ()));
561- case MEASURE :
562- return Optional .ofNullable (client .findMeasure (this .group , this .name ()));
563- default :
564- throw new IllegalStateException ("should not reach here" );
565- }
566- } catch (BanyanDBException ex ) {
567- if (ex .getStatus ().equals (Status .Code .NOT_FOUND )) {
568- return Optional .empty ();
569- }
570-
571- throw ex ;
572- }
573- }
574-
575- public MetadataCache .EntityMetadata updateRemoteSchema (BanyanDBClient client ) throws BanyanDBException {
576- switch (kind ) {
577- case STREAM :
578- return client .updateStreamMetadataCacheFromSever (this .group , this .name ());
579- case MEASURE :
580- return client .updateMeasureMetadataCacheFromSever (this .group , this .name ());
581- default :
582- throw new IllegalStateException ("should not reach here" );
583- }
584- }
585-
586542 private List <TagFamilySpec > extractTagFamilySpec (List <TagMetadata > tagMetadataList , boolean shouldAddID ) {
587543 final String indexFamily = SchemaMetadata .this .indexFamily ();
588544 final String nonIndexFamily = SchemaMetadata .this .nonIndexFamily ();
@@ -603,95 +559,6 @@ private List<TagFamilySpec> extractTagFamilySpec(List<TagMetadata> tagMetadataLi
603559 return tagFamilySpecs ;
604560 }
605561
606- /**
607- * Check if the group settings need to be updated
608- */
609- private boolean checkGroupUpdate (BanyanDBClient client ) throws BanyanDBException {
610- Group g = client .findGroup (this .group );
611- return g .getResourceOpts ().getShardNum () != this .shard
612- || g .getResourceOpts ().getSegmentInterval ().getNum () != this .segmentIntervalDays
613- || g .getResourceOpts ().getTtl ().getNum () != this .ttlDays ;
614- }
615-
616- public boolean checkResourceExistence (BanyanDBClient client ) throws BanyanDBException {
617- ResourceExist resourceExist ;
618- Group .Builder gBuilder
619- = Group .newBuilder ()
620- .setMetadata (Metadata .newBuilder ().setName (this .group ))
621- .setResourceOpts (ResourceOpts .newBuilder ()
622- .setShardNum (this .shard )
623- .setSegmentInterval (
624- IntervalRule .newBuilder ()
625- .setUnit (
626- IntervalRule .Unit .UNIT_DAY )
627- .setNum (
628- this .segmentIntervalDays ))
629- .setTtl (
630- IntervalRule .newBuilder ()
631- .setUnit (
632- IntervalRule .Unit .UNIT_DAY )
633- .setNum (
634- this .ttlDays )));
635- switch (kind ) {
636- case STREAM :
637- resourceExist = client .existStream (this .group , this .name ());
638- gBuilder .setCatalog (Catalog .CATALOG_STREAM ).build ();
639- if (!GROUP_ALIGNED .contains (this .group )) {
640- // create the group if not exist
641- if (!resourceExist .hasGroup ()) {
642- try {
643- Group g = client .define (gBuilder .build ());
644- if (g != null ) {
645- log .info ("group {} created" , g .getMetadata ().getName ());
646- }
647- } catch (BanyanDBException ex ) {
648- if (ex .getStatus ().equals (Status .Code .ALREADY_EXISTS )) {
649- log .info ("group {} already created by another OAP node" , this .group );
650- } else {
651- throw ex ;
652- }
653- }
654- } else {
655- // update the group if necessary
656- if (this .checkGroupUpdate (client )) {
657- client .update (gBuilder .build ());
658- log .info ("group {} updated" , this .group );
659- }
660- }
661- // mark the group as aligned
662- GROUP_ALIGNED .add (this .group );
663- }
664- return resourceExist .hasResource ();
665- case MEASURE :
666- resourceExist = client .existMeasure (this .group , this .name ());
667- gBuilder .setCatalog (Catalog .CATALOG_MEASURE ).build ();
668- if (!GROUP_ALIGNED .contains (this .group )) {
669- if (!resourceExist .hasGroup ()) {
670- try {
671- Group g = client .define (gBuilder .build ());
672- if (g != null ) {
673- log .info ("group {} created" , g .getMetadata ().getName ());
674- }
675- } catch (BanyanDBException ex ) {
676- if (ex .getStatus ().equals (Status .Code .ALREADY_EXISTS )) {
677- log .info ("group {} already created by another OAP node" , this .group );
678- } else {
679- throw ex ;
680- }
681- }
682- } else {
683- if (this .checkGroupUpdate (client )) {
684- client .update (gBuilder .build ());
685- log .info ("group {} updated" , this .group );
686- }
687- }
688- }
689- return resourceExist .hasResource ();
690- default :
691- throw new IllegalStateException ("should not reach here" );
692- }
693- }
694-
695562 /**
696563 * @return name of the Stream/Measure in the BanyanDB
697564 */
@@ -761,52 +628,17 @@ public static class Schema {
761628
762629 @ Getter
763630 @ Nullable
764- private final TopNSpec topNSpec ;
631+ private final TopNAggregation topNSpec ;
765632
766633 public ColumnSpec getSpec (String columnName ) {
767634 return this .specs .get (columnName );
768635 }
769636
770- public void installTopNAggregation (BanyanDBClient client ) throws BanyanDBException {
771- if (this .getTopNSpec () == null ) {
772- if (this .metadata .kind == Kind .MEASURE ) {
773- log .debug ("skip null TopN Schema for [{}]" , metadata .getModelName ());
774- }
775- return ;
776- }
777- TopNAggregation .Builder builder
778- = TopNAggregation .newBuilder ()
779- .setMetadata (Metadata .newBuilder ()
780- .setGroup (getMetadata ().getGroup ())
781- .setName (this .getTopNSpec ().getName ()))
782-
783- .setSourceMeasure (Metadata .newBuilder ()
784- .setGroup (getMetadata ().getGroup ())
785- .setName (getMetadata ().name ()))
786- .setFieldValueSort (this .getTopNSpec ().getSort ())
787- .setFieldName (this .getTopNSpec ().getFieldName ())
788- .addAllGroupByTagNames (this .getTopNSpec ().getGroupByTagNames ())
789- .setCountersNumber (this .getTopNSpec ().getCountersNumber ())
790- .setLruSize (this .getTopNSpec ().getLruSize ());
791- client .define (builder .build ());
792- log .info ("installed TopN schema for measure {}" , getMetadata ().name ());
637+ public static String formatTopNName (String measureName ) {
638+ return measureName + "_topn" ;
793639 }
794640 }
795641
796- @ Builder
797- @ EqualsAndHashCode
798- @ Getter
799- @ ToString
800- public static class TopNSpec {
801- private final String name ;
802- @ Singular
803- private final List <String > groupByTagNames ;
804- private final String fieldName ;
805- private final BanyandbModel .Sort sort ;
806- private final int lruSize ;
807- private final int countersNumber ;
808- }
809-
810642 @ RequiredArgsConstructor
811643 @ Getter
812644 @ ToString
@@ -818,11 +650,4 @@ public static class ColumnSpec {
818650 public enum ColumnType {
819651 TAG , FIELD ;
820652 }
821-
822- @ Getter
823- @ Setter
824- @ NoArgsConstructor
825- public static class GroupSetting {
826- private int segmentIntervalDays ;
827- }
828653}
0 commit comments