1313import org .apache .logging .log4j .Logger ;
1414import org .elasticsearch .common .settings .ClusterSettings ;
1515import org .elasticsearch .common .settings .Setting ;
16+ import org .elasticsearch .common .settings .Settings ;
1617import org .elasticsearch .core .Nullable ;
1718import org .elasticsearch .core .TimeValue ;
1819
2425 * This class holds the data stream global retention settings. It defines, validates and monitors the settings.
2526 * <p>
2627 * The global retention settings apply to non-system data streams that are managed by the data stream lifecycle. They consist of:
27- * - The default retention which applies to data streams that do not have a retention defined.
28- * - The max retention which applies to all data streams that do not have retention or their retention has exceeded this value.
28+ * - The default retention which applies to the backing indices of data streams that do not have a retention defined.
29+ * - The max retention which applies to backing and failure indices of data streams that do not have retention or their
30+ * retention has exceeded this value.
31+ * - The failures default retention which applied to the failure indices of data streams that do not have retention defined.
2932 */
3033public class DataStreamGlobalRetentionSettings {
3134
@@ -82,27 +85,66 @@ public Iterator<Setting<?>> settings() {
8285 Setting .Property .Dynamic
8386 );
8487
88+ static final TimeValue FAILURES_DEFAULT_RETENTION = TimeValue .timeValueDays (30 );
89+ public static final Setting <TimeValue > FAILURE_STORE_DEFAULT_RETENTION_SETTING = Setting .timeSetting (
90+ "data_streams.lifecycle.retention.failures_default" ,
91+ FAILURES_DEFAULT_RETENTION ,
92+ new Setting .Validator <>() {
93+ @ Override
94+ public void validate (TimeValue value ) {}
95+
96+ @ Override
97+ public void validate (final TimeValue settingValue , final Map <Setting <?>, Object > settings ) {
98+ TimeValue defaultRetention = getSettingValueOrNull (settingValue );
99+ // Currently, we do not validate the default for the failure store against the max because
100+ // we start with a default value that might conflict the max retention.
101+ validateIsolatedRetentionValue (defaultRetention , FAILURE_STORE_DEFAULT_RETENTION_SETTING .getKey ());
102+ }
103+ },
104+ Setting .Property .NodeScope ,
105+ Setting .Property .Dynamic
106+ );
107+
85108 @ Nullable
86109 private volatile TimeValue defaultRetention ;
87110 @ Nullable
88111 private volatile TimeValue maxRetention ;
112+ @ Nullable
113+ private volatile TimeValue failuresDefaultRetention ;
114+ /** We cache the global retention objects, volatile is sufficient we only "write" this values in the settings appliers which
115+ * are executed by {@link org.elasticsearch.common.settings.AbstractScopedSettings#applySettings(Settings)} which is synchronised.
116+ */
117+ @ Nullable
118+ private volatile DataStreamGlobalRetention dataGlobalRetention ;
119+ @ Nullable
120+ private volatile DataStreamGlobalRetention failuresGlobalRetention ;
89121
90122 private DataStreamGlobalRetentionSettings () {
91123
92124 }
93125
126+ /**
127+ * @return the max retention that applies to all data stream data
128+ */
94129 @ Nullable
95130 public TimeValue getMaxRetention () {
96131 return maxRetention ;
97132 }
98133
134+ /**
135+ * @return the default retention that applies either to the data component
136+ */
99137 @ Nullable
100138 public TimeValue getDefaultRetention () {
101139 return defaultRetention ;
102140 }
103141
104- public boolean areDefined () {
105- return getDefaultRetention () != null || getMaxRetention () != null ;
142+ /**
143+ * @return the default retention that applies either to the data or the failures component
144+ */
145+ @ Nullable
146+ public TimeValue getDefaultRetention (boolean failureStore ) {
147+ return failureStore ? failuresDefaultRetention : defaultRetention ;
106148 }
107149
108150 /**
@@ -113,17 +155,33 @@ public static DataStreamGlobalRetentionSettings create(ClusterSettings clusterSe
113155 DataStreamGlobalRetentionSettings dataStreamGlobalRetentionSettings = new DataStreamGlobalRetentionSettings ();
114156 clusterSettings .initializeAndWatch (DATA_STREAMS_DEFAULT_RETENTION_SETTING , dataStreamGlobalRetentionSettings ::setDefaultRetention );
115157 clusterSettings .initializeAndWatch (DATA_STREAMS_MAX_RETENTION_SETTING , dataStreamGlobalRetentionSettings ::setMaxRetention );
158+ clusterSettings .initializeAndWatch (
159+ FAILURE_STORE_DEFAULT_RETENTION_SETTING ,
160+ dataStreamGlobalRetentionSettings ::setFailuresDefaultRetention
161+ );
116162 return dataStreamGlobalRetentionSettings ;
117163 }
118164
119165 private void setMaxRetention (TimeValue maxRetention ) {
120166 this .maxRetention = getSettingValueOrNull (maxRetention );
121- logger .info ("Updated max factory retention to [{}]" , this .maxRetention == null ? null : maxRetention .getStringRep ());
167+ this .dataGlobalRetention = createDataStreamGlobalRetention (false );
168+ this .failuresGlobalRetention = createDataStreamGlobalRetention (true );
169+ logger .info ("Updated global max retention to [{}]" , this .maxRetention == null ? null : maxRetention .getStringRep ());
122170 }
123171
124172 private void setDefaultRetention (TimeValue defaultRetention ) {
125173 this .defaultRetention = getSettingValueOrNull (defaultRetention );
126- logger .info ("Updated default factory retention to [{}]" , this .defaultRetention == null ? null : defaultRetention .getStringRep ());
174+ this .dataGlobalRetention = createDataStreamGlobalRetention (false );
175+ logger .info ("Updated global default retention to [{}]" , this .defaultRetention == null ? null : defaultRetention .getStringRep ());
176+ }
177+
178+ private void setFailuresDefaultRetention (TimeValue failuresDefaultRetention ) {
179+ this .failuresDefaultRetention = getSettingValueOrNull (failuresDefaultRetention );
180+ this .failuresGlobalRetention = createDataStreamGlobalRetention (true );
181+ logger .info (
182+ "Updated failures default retention to [{}]" ,
183+ this .failuresDefaultRetention == null ? null : failuresDefaultRetention .getStringRep ()
184+ );
127185 }
128186
129187 private static void validateIsolatedRetentionValue (@ Nullable TimeValue retention , String settingName ) {
@@ -150,12 +208,36 @@ private static void validateGlobalRetentionConfiguration(@Nullable TimeValue def
150208 }
151209 }
152210
211+ /**
212+ * @return the global retention of backing indices
213+ */
153214 @ Nullable
154215 public DataStreamGlobalRetention get () {
155- if (areDefined () == false ) {
216+ return get (false );
217+ }
218+
219+ /**
220+ * Returns the global retention that applies to the data or failures of a data stream
221+ * @param failureStore, true if we are retrieving the global retention that applies to failure store, false otherwise.
222+ */
223+ @ Nullable
224+ public DataStreamGlobalRetention get (boolean failureStore ) {
225+ return failureStore ? failuresGlobalRetention : dataGlobalRetention ;
226+ }
227+
228+ @ Nullable
229+ private DataStreamGlobalRetention createDataStreamGlobalRetention (boolean failureStore ) {
230+ if (areDefined (failureStore ) == false ) {
156231 return null ;
157232 }
158- return new DataStreamGlobalRetention (getDefaultRetention (), getMaxRetention ());
233+ TimeValue defaultRetention = getDefaultRetention (failureStore );
234+ TimeValue maxRetention = getMaxRetention ();
235+ // We ensure that we create valid DataStreamGlobalRetention where default is less or equal to max.
236+ // If it's not we set it to null.
237+ if (defaultRetention != null && maxRetention != null && defaultRetention .getMillis () > maxRetention .getMillis ()) {
238+ return new DataStreamGlobalRetention (null , getMaxRetention ());
239+ }
240+ return new DataStreamGlobalRetention (defaultRetention , maxRetention );
159241 }
160242
161243 /**
@@ -169,4 +251,8 @@ public DataStreamGlobalRetention get() {
169251 private static TimeValue getSettingValueOrNull (TimeValue value ) {
170252 return value == null || value .equals (TimeValue .MINUS_ONE ) ? null : value ;
171253 }
254+
255+ private boolean areDefined (boolean failureStore ) {
256+ return getDefaultRetention (failureStore ) != null || getMaxRetention () != null ;
257+ }
172258}
0 commit comments