/
ServiceConfigurations.java
440 lines (387 loc) · 17.6 KB
/
ServiceConfigurations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package com.eucalyptus.component;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.NoSuchElementException;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceException;
import org.apache.log4j.Logger;
import com.eucalyptus.bootstrap.Hosts;
import com.eucalyptus.component.Faults.CheckException;
import com.eucalyptus.empyrean.ServiceId;
import com.eucalyptus.empyrean.ServiceStatusDetail;
import com.eucalyptus.empyrean.ServiceStatusType;
import com.eucalyptus.entities.Entities;
import com.eucalyptus.entities.EntityWrapper;
import com.eucalyptus.records.EventClass;
import com.eucalyptus.records.EventRecord;
import com.eucalyptus.records.EventType;
import com.eucalyptus.util.Exceptions;
import com.eucalyptus.util.Internets;
import com.eucalyptus.util.LogUtil;
import com.eucalyptus.util.TypeMapper;
import com.eucalyptus.util.TypeMappers;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
public class ServiceConfigurations {
static Logger LOG = Logger.getLogger( ServiceConfigurations.class );
public static <T extends ServiceConfiguration> List<ServiceConfiguration> list( ) throws PersistenceException {
Predicate<ServiceConfiguration> alwaysTrue = Predicates.alwaysTrue( );
return Lists.newArrayList( filter( alwaysTrue ) );
}
public static <T extends ServiceConfiguration> Iterable<ServiceConfiguration> filter( final Predicate<T> pred ) throws PersistenceException {
List<ServiceConfiguration> configs = Lists.newArrayList( );
for ( ComponentId compId : ComponentIds.list( ) ) {
Iterables.addAll( configs, filter( compId.getClass( ), pred ) );
}
return configs;
}
private enum DatabaseProvider {
INSTANCE;
public <T extends ServiceConfiguration> List<T> list( final T example ) {
final EntityTransaction db = Entities.get( example.getClass( ) );
List<T> componentList;
try {
componentList = Entities.query( example );
db.commit( );
return componentList;
} catch ( final PersistenceException ex ) {
LOG.debug( ex );
db.rollback( );
throw ex;
} catch ( final Throwable ex ) {
LOG.debug( ex );
db.rollback( );
throw new PersistenceException( "Service configuration lookup failed for: " + LogUtil.dumpObject( example ), ex );
}
}
public <T extends ServiceConfiguration> T lookup( final T example ) {
final EntityTransaction db = Entities.get( example );
T existingName = null;
try {
existingName = Entities.uniqueResult( example );
db.commit( );
return existingName;
} catch ( final NoSuchElementException ex ) {
db.rollback( );
throw ex;
} catch ( final PersistenceException ex ) {
LOG.debug( ex );
db.rollback( );
throw ex;
} catch ( final Throwable ex ) {
LOG.debug( ex );
db.rollback( );
throw new PersistenceException( "Service configuration lookup failed for: " + LogUtil.dumpObject( example ), ex );
}
}
public <T extends ServiceConfiguration> T store( T config ) {
final EntityTransaction db = Entities.get( config.getClass( ) );
try {
config = Entities.persist( config );
db.commit( );
EventRecord.here( ServiceConfigurations.class, EventClass.COMPONENT, EventType.COMPONENT_REGISTERED, config.toString( ) ).info( );
} catch ( final PersistenceException ex ) {
LOG.debug( ex );
EventRecord.here( ServiceConfigurations.class, EventClass.COMPONENT, EventType.COMPONENT_REGISTERED, "FAILED", config.toString( ) ).error( );
db.rollback( );
throw ex;
} catch ( final Throwable ex ) {
LOG.debug( ex );
EventRecord.here( ServiceConfigurations.class, EventClass.COMPONENT, EventType.COMPONENT_REGISTERED, "FAILED", config.toString( ) ).error( );
db.rollback( );
throw new PersistenceException( "Service configuration storing failed for: " + LogUtil.dumpObject( config ), ex );
}
return config;
}
public <T extends ServiceConfiguration> T remove( final T config ) {
final EntityTransaction db = Entities.get( config.getClass( ) );
try {
final T searchConfig = ( T ) config.getClass( ).newInstance( );
searchConfig.setName( config.getName( ) );
final T exists = Entities.uniqueResult( searchConfig );
Entities.delete( exists );
db.commit( );
EventRecord.here( ServiceConfigurations.class, EventClass.COMPONENT, EventType.COMPONENT_DEREGISTERED, config.toString( ) ).info( );
} catch ( final NoSuchElementException ex ) {
db.rollback( );
} catch ( final PersistenceException ex ) {
LOG.debug( ex );
EventRecord.here( ServiceConfigurations.class, EventClass.COMPONENT, EventType.COMPONENT_DEREGISTERED, "FAILED", config.toString( ) ).error( );
db.rollback( );
throw ex;
} catch ( final Throwable ex ) {
LOG.debug( ex );
EventRecord.here( ServiceConfigurations.class, EventClass.COMPONENT, EventType.COMPONENT_DEREGISTERED, "FAILED", config.toString( ) ).error( );
db.rollback( );
throw new PersistenceException( "Service configuration removal failed for: " + LogUtil.dumpObject( config ), ex );
}
return config;
}
}
public static Function<ServiceConfiguration, ServiceStatusType> asServiceStatus( ) {
return asServiceStatus( false, false );
}
public static Function<ServiceConfiguration, ServiceStatusType> asServiceStatus( final boolean showEvents, final boolean showEventStacks ) {
return new Function<ServiceConfiguration, ServiceStatusType>( ) {
@Override
public ServiceStatusType apply( final ServiceConfiguration config ) {
return new ServiceStatusType( ) {
/**
*
*/
private static final long serialVersionUID = 1L;
{
this.setServiceId( TypeMappers.transform( config, ServiceId.class ) );
this.setLocalEpoch( Topology.epoch( ) );
try {
this.setLocalState( config.lookupState( ).toString( ) );
} catch ( final Exception ex ) {
this.setLocalState( "n/a: " + ex.getMessage( ) );
}
if ( showEvents ) {
this.getStatusDetails( ).addAll( Collections2.transform( Faults.lookup( config ),
TypeMappers.lookup( CheckException.class, ServiceStatusDetail.class ) ) );
if ( !showEventStacks ) {
for ( final ServiceStatusDetail a : this.getStatusDetails( ) ) {
a.setStackTrace( "" );
}
}
}
}
};
}
};
}
@TypeMapper
enum ServiceConfigurationToStatus implements Function<ServiceConfiguration, ServiceStatusType> {
INSTANCE;
@Override
public ServiceStatusType apply( final ServiceConfiguration config ) {
return new ServiceStatusType( ) {
{
this.setServiceId( TypeMappers.transform( config, ServiceId.class ) );
this.setLocalEpoch( Topology.epoch( ) );
if ( config.isVmLocal( ) && Faults.isFailstop( ) ) {
this.setLocalState( Component.State.NOTREADY.name( ) );
} else {
try {
this.setLocalState( config.lookupState( ).toString( ) );
} catch ( final Exception ex ) {
this.setLocalState( "n/a: " + ex.getMessage( ) );
}
}
this.getStatusDetails( ).addAll( Collections2.transform( Faults.lookup( config ),
TypeMappers.lookup( CheckException.class, ServiceStatusDetail.class ) ) );
for ( final ServiceStatusDetail a : this.getStatusDetails( ) ) {
a.setStackTrace( "" );
}
}
};
}
};
@TypeMapper
public enum ServiceIdToServiceStatus implements Function<ServiceId, ServiceStatusType> {
INSTANCE;
private final Function<ServiceId, ServiceStatusType> transform = Functions.compose( ServiceConfigurationToStatus.INSTANCE, ServiceIdToServiceConfiguration.INSTANCE );
@Override
public ServiceStatusType apply( ServiceId input ) {
return transform.apply( input );
}
}
@TypeMapper
public enum ServiceIdToServiceConfiguration implements Function<ServiceId, ServiceConfiguration> {
INSTANCE;
@Override
public ServiceConfiguration apply( final ServiceId arg0 ) {
final Component comp = Components.lookup( arg0.getType( ) );
try {
return comp.lookup( arg0.getName( ) );
} catch ( final NoSuchElementException ex1 ) {
final ServiceBuilder<? extends ServiceConfiguration> builder = ServiceBuilders.lookup( comp.getComponentId( ) );
try {
final URI uri = new URI( arg0.getUri( ) );
ServiceConfiguration config = builder.newInstance( arg0.getPartition( ), arg0.getName( ), uri.getHost( ), uri.getPort( ) );
comp.setup( config );
return config;
} catch ( final URISyntaxException ex ) {
LOG.error( ex, ex );
throw Exceptions.toUndeclared( ex );
}
}
}
}
@TypeMapper
public enum ServiceConfigurationToServiceId implements Function<ServiceConfiguration, ServiceId> {
INSTANCE;
@Override
public ServiceId apply( final ServiceConfiguration arg0 ) {
return new ServiceId( ) {
/**
*
*/
private static final long serialVersionUID = 1L;
{
this.setPartition( arg0.getPartition( ) );
this.setName( arg0.getName( ) );
this.setType( arg0.getComponentId( ).name( ) );
this.setFullName( arg0.getFullName( ).toString( ) );
if ( arg0.isVmLocal( ) ) {
this.setUri( ServiceUris.remote( arg0.getComponentId( ) ).toASCIIString( ) );
} else {
this.setUri( ServiceUris.remote( arg0 ).toASCIIString( ) );
}
}
};
}
}
public static ServiceConfiguration createEphemeral( final ComponentId compId, final String partition, final String name, final URI remoteUri ) {
return new EphemeralConfiguration( compId, partition, name, remoteUri );
}
public static ServiceConfiguration createEphemeral( final ComponentId compId ) {
return createEphemeral( compId, Internets.localHostInetAddress( ) );
}
public static ServiceConfiguration createEphemeral( final ComponentId compId, final InetAddress host ) {
return new EphemeralConfiguration( compId, compId.getPartition( ), host.getHostAddress( ), ServiceUris.remote( compId, host ) );
}
public static ServiceConfiguration createEphemeral( final Component component, final InetAddress host ) {
return createEphemeral( component.getComponentId( ), host );
}
public static ServiceConfiguration createBogus( final Class<? extends ComponentId> compIdClass, final Class<?> ownerType ) {
ComponentId compId = ComponentIds.lookup( compIdClass );
return new EphemeralConfiguration( compId, compId.getPartition( ), ownerType.getCanonicalName( ), ServiceUris.internal( compId, Internets.localHostInetAddress( ), ownerType.getSimpleName( ) ) );
}
public static <T extends ServiceConfiguration, C extends ComponentId> Iterable<T> filter( final Class<C> type, final Predicate<T> pred ) throws PersistenceException {
List<T> list = ServiceConfigurations.list( type );
return Iterables.filter( list, pred );
}
public static <T extends ServiceConfiguration, C extends ComponentId> List<T> list( final Class<C> type ) throws PersistenceException {
if ( !ComponentId.class.isAssignableFrom( type ) ) {
throw new PersistenceException( "Unknown configuration type passed: " + type.getCanonicalName( ) );
} else {
final T example = ( T ) ServiceBuilders.lookup( type ).newInstance( );
return ServiceConfigurations.list( example );
}
}
public static <T extends ServiceConfiguration, C extends ComponentId> List<T> listPartition( final Class<C> type, final String partition ) throws PersistenceException, NoSuchElementException {
if ( !ComponentId.class.isAssignableFrom( type ) ) {
throw new PersistenceException( "Unknown configuration type passed: " + type.getCanonicalName( ) );
}
final T example = ( T ) ServiceBuilders.lookup( type ).newInstance( );
example.setPartition( partition );
return list( example );
}
public static <T extends ServiceConfiguration> T lookupByName( final String name ) {
for ( ComponentId c : ComponentIds.list( ) ) {
ServiceConfiguration example = ServiceBuilders.lookup( c.getClass( ) ).newInstance( );
example.setName( name );
try {
return ( T ) lookup( example );
} catch ( Exception ex ) {}
}
throw new NoSuchElementException( "Failed to lookup any registered component with the name: " + name );
}
public static <T extends ServiceConfiguration, C extends ComponentId> T lookupByName( final Class<C> type, final String name ) {
if ( !ComponentId.class.isAssignableFrom( type ) ) {
throw new PersistenceException( "Unknown configuration type passed: " + type.getCanonicalName( ) );
}
final T example = ( T ) ServiceBuilders.lookup( type ).newInstance( );
example.setName( name );
return lookup( example );
}
public static <T extends ServiceConfiguration, C extends ComponentId> T lookupByHost( final Class<C> type, final String host ) {
if ( !ComponentId.class.isAssignableFrom( type ) ) {
throw new PersistenceException( "Unknown configuration type passed: " + type.getCanonicalName( ) );
}
final T example = ( T ) ServiceBuilders.lookup( type ).newInstance( );
example.setHostName( host );
return lookup( example );
}
public static <T extends ServiceConfiguration, C extends ComponentId> T lookupByAliasHost( final Class<C> type, final String host ) {
if ( !ComponentId.class.isAssignableFrom( type ) ) {
throw new PersistenceException( "Unknown configuration type passed: " + type.getCanonicalName( ) );
}
final T example = ( T ) ServiceBuilders.lookup( type ).newInstance( );
example.setHostName( host );
return lookup( example );
}
public static <T extends ServiceConfiguration> List<T> list( final T type ) {
return DatabaseProvider.INSTANCE.list( type );
}
public static <T extends ServiceConfiguration> T store( final T t ) {
return DatabaseProvider.INSTANCE.store( t );
}
public static <T extends ServiceConfiguration> T remove( final T t ) {
return DatabaseProvider.INSTANCE.remove( t );
}
public static <T extends ServiceConfiguration> T lookup( final T type ) {
return DatabaseProvider.INSTANCE.lookup( type );
}
enum ServiceIsHostLocal implements Predicate<ServiceConfiguration> {
INSTANCE;
@Override
public boolean apply( ServiceConfiguration input ) {
return Hosts.isServiceLocal( input );
}
}
enum EnabledServiceConfiguration implements Predicate<ServiceConfiguration> {
INSTANCE;
@Override
public boolean apply( final ServiceConfiguration arg0 ) {
return Component.State.ENABLED.equals( arg0.lookupState( ) );
}
};
@SuppressWarnings( "unchecked" )
public static final <T extends ServiceConfiguration> Predicate<T> filterEnabled( ) {
return ( Predicate<T> ) EnabledServiceConfiguration.INSTANCE;
}
public static Predicate<ServiceConfiguration> filterHostLocal( ) {
return ServiceIsHostLocal.INSTANCE;
}
public static Predicate<ServiceConfiguration> filterByPartition( final Partition partition ) {
return new Predicate<ServiceConfiguration>( ) {
@Override
public boolean apply( ServiceConfiguration arg0 ) {
return partition.getName( ).equals( arg0.getPartition( ) ) && Component.State.ENABLED.equals( arg0.lookupState( ) );
}
};
}
@TypeMapper
public enum CheckExceptionRecordMapper implements Function<CheckException, ServiceStatusDetail> {
INSTANCE;
@Override
public ServiceStatusDetail apply( final CheckException input ) {
final ServiceConfiguration config = ServiceConfigurations.lookupByName( input.getServiceName( ) );
return new ServiceStatusDetail( ) {
{
this.setSeverity( input.getSeverity( ).toString( ) );
this.setUuid( input.getCorrelationId( ) );
this.setTimestamp( input.getTimestamp( ).toString( ) );
this.setMessage( input.getMessage( ) != null
? input.getMessage( )
: "No summary information available." );
this.setStackTrace( input.getStackString( ) != null
? input.getStackString( )
: Exceptions.string( new RuntimeException( "Error while mapping service event record: No stack information available" ) ) );
this.setServiceFullName( config.getFullName( ).toString( ) );
this.setServiceHost( config.getHostName( ) );
this.setServiceName( input.getServiceName( ) );
}
};
}
}
@TypeMapper
public enum ServiceBuilderMapper implements Function<ServiceConfiguration, ServiceBuilder<? extends ServiceConfiguration>> {
INSTANCE;
@Override
public ServiceBuilder<? extends ServiceConfiguration> apply( final ServiceConfiguration input ) {
return ServiceBuilders.lookup( input.getComponentId( ) );
}
}
}