|
| 1 | +package com.launchdarkly.sdk.server; |
| 2 | + |
| 3 | +import com.launchdarkly.logging.LDLogger; |
| 4 | +import com.launchdarkly.sdk.server.interfaces.DataSourceStatusProvider; |
| 5 | +import com.launchdarkly.sdk.server.interfaces.DataStoreStatusProvider; |
| 6 | +import com.launchdarkly.sdk.server.interfaces.FlagChangeEvent; |
| 7 | +import com.launchdarkly.sdk.server.interfaces.FlagChangeListener; |
| 8 | +import com.launchdarkly.sdk.server.subsystems.ComponentConfigurer; |
| 9 | +import com.launchdarkly.sdk.server.subsystems.DataSource; |
| 10 | +import com.launchdarkly.sdk.server.subsystems.DataStore; |
| 11 | +import com.launchdarkly.sdk.server.subsystems.LoggingConfiguration; |
| 12 | + |
| 13 | +import java.io.Closeable; |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.concurrent.Future; |
| 16 | + |
| 17 | +/** |
| 18 | + * Internal implementation of the FDv1 data system. |
| 19 | + * <p> |
| 20 | + * This class is package-private and should not be used by application code. |
| 21 | + */ |
| 22 | +final class FDv1DataSystem implements DataSystem, Closeable { |
| 23 | + private final DataSource dataSource; |
| 24 | + private final DataStore dataStore; |
| 25 | + private final ReadOnlyStore store; |
| 26 | + private final FlagChangeNotifier flagChanged; |
| 27 | + private final DataSourceStatusProvider dataSourceStatusProvider; |
| 28 | + private final DataStoreStatusProvider dataStoreStatusProvider; |
| 29 | + private boolean disposed = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * Testing access to internal components. |
| 33 | + */ |
| 34 | + static final class TestingAccess { |
| 35 | + final DataSource dataSource; |
| 36 | + |
| 37 | + TestingAccess(DataSource dataSource) { |
| 38 | + this.dataSource = dataSource; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + final TestingAccess testing; |
| 43 | + |
| 44 | + /** |
| 45 | + * Gets the underlying data store. This is needed for the evaluator. |
| 46 | + * @return the underlying data store |
| 47 | + */ |
| 48 | + DataStore getUnderlyingStore() { |
| 49 | + return dataStore; |
| 50 | + } |
| 51 | + |
| 52 | + private FDv1DataSystem( |
| 53 | + DataStore store, |
| 54 | + DataStoreStatusProvider dataStoreStatusProvider, |
| 55 | + DataSourceStatusProvider dataSourceStatusProvider, |
| 56 | + DataSource dataSource, |
| 57 | + FlagChangeNotifier flagChanged |
| 58 | + ) { |
| 59 | + this.dataStoreStatusProvider = dataStoreStatusProvider; |
| 60 | + this.dataSourceStatusProvider = dataSourceStatusProvider; |
| 61 | + this.store = new ReadonlyStoreFacade(store); |
| 62 | + this.flagChanged = flagChanged; |
| 63 | + this.dataSource = dataSource; |
| 64 | + this.dataStore = store; |
| 65 | + this.testing = new TestingAccess(dataSource); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a new FDv1DataSystem instance. |
| 70 | + * |
| 71 | + * @param logger the logger |
| 72 | + * @param config the SDK configuration |
| 73 | + * @param clientContext the client context |
| 74 | + * @param logConfig the logging configuration |
| 75 | + * @return a new FDv1DataSystem instance |
| 76 | + */ |
| 77 | + static FDv1DataSystem create( |
| 78 | + LDLogger logger, |
| 79 | + LDConfig config, |
| 80 | + ClientContextImpl clientContext, |
| 81 | + LoggingConfiguration logConfig |
| 82 | + ) { |
| 83 | + DataStoreUpdatesImpl dataStoreUpdates = new DataStoreUpdatesImpl( |
| 84 | + EventBroadcasterImpl.forDataStoreStatus(clientContext.sharedExecutor, logger)); |
| 85 | + |
| 86 | + DataStore dataStore = (config.dataStore == null ? Components.inMemoryDataStore() : config.dataStore) |
| 87 | + .build(clientContext.withDataStoreUpdateSink(dataStoreUpdates)); |
| 88 | + |
| 89 | + DataStoreStatusProvider dataStoreStatusProvider = new DataStoreStatusProviderImpl(dataStore, dataStoreUpdates); |
| 90 | + |
| 91 | + // Create a single flag change broadcaster to be shared between DataSourceUpdatesImpl and FlagTrackerImpl |
| 92 | + EventBroadcasterImpl<FlagChangeListener, FlagChangeEvent> flagChangeBroadcaster = |
| 93 | + EventBroadcasterImpl.forFlagChangeEvents(clientContext.sharedExecutor, logger); |
| 94 | + |
| 95 | + // Create a single data source status broadcaster to be shared between DataSourceUpdatesImpl and DataSourceStatusProviderImpl |
| 96 | + EventBroadcasterImpl<DataSourceStatusProvider.StatusListener, DataSourceStatusProvider.Status> dataSourceStatusBroadcaster = |
| 97 | + EventBroadcasterImpl.forDataSourceStatus(clientContext.sharedExecutor, logger); |
| 98 | + |
| 99 | + DataSourceUpdatesImpl dataSourceUpdates = new DataSourceUpdatesImpl( |
| 100 | + dataStore, |
| 101 | + dataStoreStatusProvider, |
| 102 | + flagChangeBroadcaster, |
| 103 | + dataSourceStatusBroadcaster, |
| 104 | + clientContext.sharedExecutor, |
| 105 | + logConfig.getLogDataSourceOutageAsErrorAfter(), |
| 106 | + logger |
| 107 | + ); |
| 108 | + |
| 109 | + ComponentConfigurer<DataSource> dataSourceFactory = config.offline |
| 110 | + ? Components.externalUpdatesOnly() |
| 111 | + : (config.dataSource == null ? Components.streamingDataSource() : config.dataSource); |
| 112 | + DataSource dataSource = dataSourceFactory.build(clientContext.withDataSourceUpdateSink(dataSourceUpdates)); |
| 113 | + DataSourceStatusProvider dataSourceStatusProvider = new DataSourceStatusProviderImpl( |
| 114 | + dataSourceStatusBroadcaster, |
| 115 | + dataSourceUpdates); |
| 116 | + |
| 117 | + FlagChangeNotifier flagChanged = new FlagChangedFacade(dataSourceUpdates); |
| 118 | + |
| 119 | + return new FDv1DataSystem( |
| 120 | + dataStore, |
| 121 | + dataStoreStatusProvider, |
| 122 | + dataSourceStatusProvider, |
| 123 | + dataSource, |
| 124 | + flagChanged |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public ReadOnlyStore getStore() { |
| 130 | + return store; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Future<Void> start() { |
| 135 | + return dataSource.start(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public boolean isInitialized() { |
| 140 | + return dataSource.isInitialized(); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public FlagChangeNotifier getFlagChanged() { |
| 145 | + return flagChanged; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public DataSourceStatusProvider getDataSourceStatusProvider() { |
| 150 | + return dataSourceStatusProvider; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public DataStoreStatusProvider getDataStoreStatusProvider() { |
| 155 | + return dataStoreStatusProvider; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void close() throws IOException { |
| 160 | + if (disposed) { |
| 161 | + return; |
| 162 | + } |
| 163 | + try { |
| 164 | + if (dataSource instanceof Closeable) { |
| 165 | + ((Closeable) dataSource).close(); |
| 166 | + } |
| 167 | + if (dataStore instanceof Closeable) { |
| 168 | + ((Closeable) dataStore).close(); |
| 169 | + } |
| 170 | + // DataSourceUpdatesImpl doesn't implement Closeable |
| 171 | + } finally { |
| 172 | + disposed = true; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
0 commit comments