2828import java .util .ArrayList ;
2929import java .util .Comparator ;
3030import java .util .HashMap ;
31+ import java .util .HashSet ;
3132import java .util .List ;
3233import java .util .Map ;
3334import java .util .Objects ;
@@ -93,6 +94,8 @@ public enum MetadataRegistry {
9394 INSTANCE ;
9495
9596 private static final ObjectMapper MAPPER = new ObjectMapper ();
97+ // BanyanDB group setting aligned with the OAP settings
98+ private static final Set <String > GROUP_ALIGNED = new HashSet <>();
9699 private final Map <String , Schema > registry = new HashMap <>();
97100
98101 private Map <String , GroupSetting > specificGroupSettings = new HashMap <>();
@@ -609,6 +612,16 @@ private List<TagFamilySpec> extractTagFamilySpec(List<TagMetadata> tagMetadataLi
609612 return tagFamilySpecs ;
610613 }
611614
615+ /**
616+ * Check if the group settings need to be updated
617+ */
618+ private boolean checkGroupUpdate (BanyanDBClient client ) throws BanyanDBException {
619+ Group g = client .findGroup (this .group );
620+ return g .getResourceOpts ().getShardNum () != this .shard
621+ || g .getResourceOpts ().getSegmentInterval ().getNum () != this .segmentIntervalDays
622+ || g .getResourceOpts ().getTtl ().getNum () != this .ttlDays ;
623+ }
624+
612625 public boolean checkResourceExistence (BanyanDBClient client ) throws BanyanDBException {
613626 ResourceExist resourceExist ;
614627 Group .Builder gBuilder
@@ -631,36 +644,57 @@ public boolean checkResourceExistence(BanyanDBClient client) throws BanyanDBExce
631644 switch (kind ) {
632645 case STREAM :
633646 resourceExist = client .existStream (this .group , this .name ());
634- if (!resourceExist .hasGroup ()) {
635- try {
636- Group g = client .define (gBuilder .setCatalog (Catalog .CATALOG_STREAM ).build ());
637- if (g != null ) {
638- log .info ("group {} created" , g .getMetadata ().getName ());
647+ gBuilder .setCatalog (Catalog .CATALOG_STREAM ).build ();
648+ if (!GROUP_ALIGNED .contains (this .group )) {
649+ // create the group if not exist
650+ if (!resourceExist .hasGroup ()) {
651+ try {
652+ Group g = client .define (gBuilder .build ());
653+ if (g != null ) {
654+ log .info ("group {} created" , g .getMetadata ().getName ());
655+ }
656+ } catch (BanyanDBException ex ) {
657+ if (ex .getStatus ().equals (Status .Code .ALREADY_EXISTS )) {
658+ log .info ("group {} already created by another OAP node" , this .group );
659+ } else {
660+ throw ex ;
661+ }
639662 }
640- } catch ( BanyanDBException ex ) {
641- if ( ex . getStatus (). equals ( Status . Code . ALREADY_EXISTS )) {
642- log . info ( "group {} already created by another OAP node" , this .group );
643- } else {
644- throw ex ;
663+ } else {
664+ // update the group if necessary
665+ if ( this .checkGroupUpdate ( client )) {
666+ client . update ( gBuilder . build ());
667+ log . info ( "group {} updated" , this . group ) ;
645668 }
646669 }
670+ // mark the group as aligned
671+ GROUP_ALIGNED .add (this .group );
647672 }
648673 return resourceExist .hasResource ();
649674 case MEASURE :
650675 resourceExist = client .existMeasure (this .group , this .name ());
651- try {
676+ gBuilder .setCatalog (Catalog .CATALOG_MEASURE ).build ();
677+ if (!GROUP_ALIGNED .contains (this .group )) {
652678 if (!resourceExist .hasGroup ()) {
653- Group g = client .define (gBuilder .setCatalog (Catalog .CATALOG_MEASURE ).build ());
654- if (g != null ) {
655- log .info ("group {} created" , g .getMetadata ().getName ());
679+ try {
680+ Group g = client .define (gBuilder .build ());
681+ if (g != null ) {
682+ log .info ("group {} created" , g .getMetadata ().getName ());
683+ }
684+ } catch (BanyanDBException ex ) {
685+ if (ex .getStatus ().equals (Status .Code .ALREADY_EXISTS )) {
686+ log .info ("group {} already created by another OAP node" , this .group );
687+ } else {
688+ throw ex ;
689+ }
656690 }
657- }
658- } catch (BanyanDBException ex ) {
659- if (ex .getStatus ().equals (Status .Code .ALREADY_EXISTS )) {
660- log .info ("group {} already created by another OAP node" , this .group );
661691 } else {
662- throw ex ;
692+ if (this .checkGroupUpdate (client )) {
693+ client .update (gBuilder .build ());
694+ log .info ("group {} updated" , this .group );
695+ }
663696 }
697+ GROUP_ALIGNED .add (this .group );
664698 }
665699 return resourceExist .hasResource ();
666700 default :
0 commit comments