1+ package org .apache .atlas .services ;
2+
3+ import org .apache .atlas .exception .AtlasBaseException ;
4+ import org .apache .atlas .repository .Constants ;
5+ import org .apache .atlas .repository .graphdb .AtlasGraph ;
6+ import org .apache .atlas .repository .graphdb .AtlasGraphQuery ;
7+ import org .apache .atlas .repository .graphdb .AtlasVertex ;
8+ import org .apache .atlas .repository .store .graph .v2 .AtlasGraphUtilsV2 ;
9+ import org .apache .atlas .service .FeatureFlag ;
10+ import org .apache .atlas .service .FeatureFlagStore ;
11+ import org .apache .atlas .typesystem .types .DataTypes ;
12+ import org .slf4j .Logger ;
13+ import org .slf4j .LoggerFactory ;
14+ import org .springframework .stereotype .Component ;
15+
16+ import javax .annotation .PostConstruct ;
17+ import javax .inject .Inject ;
18+ import java .util .Iterator ;
19+
20+
21+ /**
22+ * Automatically enables Tag V2 for new Atlas instances
23+ * where no classification types are present. This ensures new customer tenants get the
24+ * latest tag propagation version by default.
25+ */
26+ @ Component
27+ public class TagsV2AutoEnabler {
28+ private static final Logger LOG = LoggerFactory .getLogger (TagsV2AutoEnabler .class );
29+
30+ private static final String ENABLE_JANUS_OPTIMISATION_KEY = FeatureFlag .ENABLE_JANUS_OPTIMISATION .getKey ();
31+
32+ // Property keys for type system vertices
33+ private static final String VERTEX_TYPE_PROPERTY_KEY = Constants .VERTEX_TYPE_PROPERTY_KEY ;
34+ private static final String TYPE_CATEGORY_PROPERTY_KEY = Constants .TYPE_CATEGORY_PROPERTY_KEY ;
35+ private static final String VERTEX_TYPE = AtlasGraphUtilsV2 .VERTEX_TYPE ; // "typeSystem"
36+
37+ private final AtlasGraph graph ;
38+
39+ @ Inject
40+ public TagsV2AutoEnabler (AtlasGraph graph ) {
41+ this .graph = graph ;
42+ }
43+
44+ @ PostConstruct
45+ public void checkAndEnableJanusOptimisation () throws AtlasBaseException {
46+ try {
47+ LOG .info ("Starting auto-enable check for Janus optimization..." );
48+
49+ boolean isTagV2Enabled = FeatureFlagStore .isTagV2Enabled ();
50+ if (isTagV2Enabled ) {
51+ LOG .info ("Tags v2 optimization is already enabled, skipping auto-enable check" );
52+ return ;
53+ }
54+
55+ // Check if there are any classification types present
56+ boolean hasClassificationTypes = hasClassificationTypes ();
57+
58+ if (!hasClassificationTypes ) {
59+ LOG .info ("No classification types found - enabling Tags V2 for new tenant" );
60+ FeatureFlagStore .setFlag (ENABLE_JANUS_OPTIMISATION_KEY , "true" );
61+ LOG .info ("Successfully enabled Tags V2 feature flag" );
62+ } else {
63+ LOG .info ("Classification types found - keeping existing configuration (Tags v2 disabled)" );
64+ }
65+
66+ } catch (Exception e ) {
67+ LOG .error ("Error during auto-enable check for Tags v2 optimization" , e );
68+ throw e ;
69+ }
70+ }
71+
72+ /**
73+ * Optimized method to check if any classification types exist
74+ * Uses direct graph query instead of loading all typedefs
75+ * @return true if classification types exist, false otherwise
76+ */
77+ private boolean hasClassificationTypes () throws AtlasBaseException {
78+ try {
79+ // Query for any vertex that is a type system vertex with TRAIT category
80+ AtlasGraphQuery query = graph .query ().has (VERTEX_TYPE_PROPERTY_KEY , VERTEX_TYPE )
81+ .has (TYPE_CATEGORY_PROPERTY_KEY , DataTypes .TypeCategory .TRAIT );
82+
83+ Iterator <AtlasVertex > results = query .vertices ().iterator ();
84+ boolean hasClassifications = results .hasNext ();
85+
86+ LOG .info ("Found classification types: {}" , hasClassifications );
87+ return hasClassifications ;
88+ } catch (Exception e ) {
89+ LOG .error ("Error checking for classification types" , e );
90+ throw new AtlasBaseException ("Error checking for classification types" , e );
91+ }
92+ }
93+ }
0 commit comments