-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add di and some cleanup (#19)
* refactor: add di and some cleanup * test: add client config registry test
- Loading branch information
1 parent
2fcf57f
commit 2bafdd4
Showing
22 changed files
with
551 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
query-service-impl/src/main/java/org/hypertrace/core/query/service/QueryServiceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.hypertrace.core.query.service; | ||
|
||
import com.google.inject.Guice; | ||
import com.typesafe.config.Config; | ||
import org.hypertrace.core.query.service.api.QueryServiceGrpc.QueryServiceImplBase; | ||
|
||
public class QueryServiceFactory { | ||
|
||
public static QueryServiceImplBase build(Config config) { | ||
return Guice.createInjector(new QueryServiceModule(config)) | ||
.getInstance(QueryServiceImplBase.class); | ||
} | ||
} |
56 changes: 10 additions & 46 deletions
56
query-service-impl/src/main/java/org/hypertrace/core/query/service/QueryServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
query-service-impl/src/main/java/org/hypertrace/core/query/service/QueryServiceModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.hypertrace.core.query.service; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.typesafe.config.Config; | ||
import org.hypertrace.core.query.service.api.QueryServiceGrpc.QueryServiceImplBase; | ||
import org.hypertrace.core.query.service.pinot.PinotModule; | ||
|
||
class QueryServiceModule extends AbstractModule { | ||
|
||
private final QueryServiceImplConfig config; | ||
|
||
QueryServiceModule(Config config) { | ||
this.config = QueryServiceImplConfig.parse(config); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
bind(QueryServiceImplConfig.class).toInstance(this.config); | ||
bind(QueryServiceImplBase.class).to(QueryServiceImpl.class); | ||
install(new PinotModule()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ice-impl/src/main/java/org/hypertrace/core/query/service/RequestClientConfigRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.hypertrace.core.query.service; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import javax.inject.Inject; | ||
import org.hypertrace.core.query.service.QueryServiceImplConfig.ClientConfig; | ||
|
||
public class RequestClientConfigRegistry { | ||
private final Map<String, ClientConfig> clientConfigMap; | ||
|
||
@Inject | ||
RequestClientConfigRegistry(QueryServiceImplConfig queryServiceImplConfig) { | ||
this.clientConfigMap = | ||
queryServiceImplConfig.getClients().stream() | ||
.map(ClientConfig::parse) | ||
.collect(Collectors.toUnmodifiableMap(ClientConfig::getType, Function.identity())); | ||
} | ||
|
||
public Optional<ClientConfig> get(String key) { | ||
return Optional.ofNullable(this.clientConfigMap.get(key)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...y-service-impl/src/main/java/org/hypertrace/core/query/service/RequestHandlerBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.hypertrace.core.query.service; | ||
|
||
import org.hypertrace.core.query.service.QueryServiceImplConfig.RequestHandlerConfig; | ||
|
||
public interface RequestHandlerBuilder { | ||
|
||
boolean canBuild(RequestHandlerConfig config); | ||
|
||
RequestHandler<?, ?> build(RequestHandlerConfig config); | ||
} |
58 changes: 33 additions & 25 deletions
58
...-service-impl/src/main/java/org/hypertrace/core/query/service/RequestHandlerRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,42 @@ | ||
package org.hypertrace.core.query.service; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import org.hypertrace.core.query.service.QueryServiceImplConfig.RequestHandlerConfig; | ||
|
||
@Singleton | ||
public class RequestHandlerRegistry { | ||
|
||
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(RequestHandlerRegistry.class); | ||
|
||
Map<String, RequestHandlerInfo> requestHandlerInfoMap = new HashMap<>(); | ||
|
||
private static final RequestHandlerRegistry INSTANCE = new RequestHandlerRegistry(); | ||
|
||
private RequestHandlerRegistry() {} | ||
|
||
public boolean register(String handlerName, RequestHandlerInfo requestHandlerInfo) { | ||
if (requestHandlerInfoMap.containsKey(handlerName)) { | ||
LOG.error("RequestHandlerInfo registration failed. Duplicate Handler:{} ", handlerName); | ||
return false; | ||
} | ||
requestHandlerInfoMap.put(handlerName, requestHandlerInfo); | ||
return true; | ||
private final Set<RequestHandler<?, ?>> requestHandlers; | ||
|
||
@Inject | ||
RequestHandlerRegistry( | ||
QueryServiceImplConfig config, Set<RequestHandlerBuilder> requestHandlerInfoSet) { | ||
this.requestHandlers = | ||
config.getQueryRequestHandlersConfig().stream() | ||
.map(RequestHandlerConfig::parse) | ||
.map(handlerConfig -> buildFromMatchingHandler(requestHandlerInfoSet, handlerConfig)) | ||
.collect( | ||
Collectors.collectingAndThen( | ||
Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet)); | ||
} | ||
|
||
public Collection<RequestHandlerInfo> getAll() { | ||
return requestHandlerInfoMap.values(); | ||
public Set<RequestHandler<?, ?>> getAll() { | ||
return requestHandlers; | ||
} | ||
|
||
public static RequestHandlerRegistry get() { | ||
return INSTANCE; | ||
private RequestHandler<?, ?> buildFromMatchingHandler( | ||
Set<RequestHandlerBuilder> handlerInfoBuilders, RequestHandlerConfig config) { | ||
return handlerInfoBuilders.stream() | ||
.filter(builder -> builder.canBuild(config)) | ||
.findFirst() | ||
.map(builder -> builder.build(config)) | ||
.orElseThrow( | ||
() -> | ||
new UnsupportedOperationException( | ||
"No builder registered matching provided config: " + config.toString())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.