|
3 | 3 | import com.datastax.oss.driver.api.core.CqlSession;
|
4 | 4 | import com.datastax.oss.driver.api.core.cql.ResultSet;
|
5 | 5 | import com.datastax.oss.driver.api.core.cql.Row;
|
| 6 | +import com.datastax.oss.driver.api.core.cql.SimpleStatement; |
| 7 | +import com.datastax.oss.driver.api.core.data.UdtValue; |
| 8 | +import demo.bank.Balance; |
6 | 9 | import demo.bank.CasaAccount;
|
7 | 10 | import demo.bank.CasaAccountServiceGrpc;
|
8 | 11 | import demo.bank.GetCasaAccountRequest;
|
| 12 | +import io.grpc.Status; |
9 | 13 | import io.grpc.stub.StreamObserver;
|
10 |
| -import io.micronaut.context.annotation.Value; |
11 |
| -import io.opencensus.exporter.trace.jaeger.JaegerExporterConfiguration; |
12 |
| -import io.opencensus.exporter.trace.jaeger.JaegerTraceExporter; |
13 |
| -import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration; |
14 |
| -import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter; |
15 |
| -import io.opencensus.trace.AttributeValue; |
16 |
| -import io.opencensus.trace.Tracing; |
17 |
| -import io.opencensus.trace.config.TraceConfig; |
18 |
| -import io.opencensus.trace.samplers.Samplers; |
19 | 14 | import org.slf4j.Logger;
|
20 | 15 | import org.slf4j.LoggerFactory;
|
21 | 16 |
|
22 |
| -import javax.annotation.PostConstruct; |
23 |
| -import javax.annotation.PreDestroy; |
| 17 | +import javax.inject.Inject; |
24 | 18 | import javax.inject.Singleton;
|
25 |
| -import java.io.IOException; |
26 |
| -import java.net.InetSocketAddress; |
27 |
| -import java.nio.file.Paths; |
28 |
| -import java.util.Map; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +import static java.util.stream.Collectors.toSet; |
29 | 22 |
|
30 | 23 | @Singleton
|
31 | 24 | public class CasaAccountServiceImpl extends CasaAccountServiceGrpc.CasaAccountServiceImplBase {
|
32 | 25 |
|
33 | 26 | private static final Logger logger = LoggerFactory.getLogger(CasaAccountServiceImpl.class);
|
34 | 27 |
|
35 |
| - @Value("${micronaut.application.name}") |
36 |
| - private String appName; |
37 |
| - |
38 |
| - @Value("${tracing.jaeger-endpoint:off}") |
39 |
| - private String jaegerThriftEndpoint; |
40 |
| - |
41 |
| - @Value("${tracing.use-stackdriver:false}") |
42 |
| - private String stackdriverFlag; |
43 |
| - |
44 |
| - @Value("${cassandra.instance:astra}") |
45 |
| - private String cassandraInstance; |
46 |
| - |
47 |
| - @Value("${cassandra.username:vino9}") |
48 |
| - private String dbUsername; |
49 |
| - |
50 |
| - @Value("${cassandra.password}") |
51 |
| - private String dbPassword; |
52 |
| - |
53 |
| - private CqlSession session; |
| 28 | + @Inject private CqlSession session; |
54 | 29 |
|
55 | 30 | public void getAccount(GetCasaAccountRequest req, StreamObserver<CasaAccount> responseObserver) {
|
56 | 31 | String accountId = req.getAccountId();
|
57 |
| - |
58 |
| - CasaAccount account = |
59 |
| - CasaAccount.newBuilder() |
60 |
| - .setAccountId(accountId) |
61 |
| - .setNickname("dummy-v1") |
62 |
| - .setStatus(CasaAccount.Status.DORMANT) |
63 |
| - .build(); |
64 |
| - responseObserver.onNext(account); |
| 32 | + retrieveAccountFromDB(accountId, responseObserver); |
65 | 33 | responseObserver.onCompleted();
|
66 |
| - |
67 |
| - logger.info("Retrieving CasaAccount details for {}", accountId); |
68 |
| - } |
69 |
| - |
70 |
| - // this will only work if there is only one gRPC service in the application |
71 |
| - // it's probably better to move this out to a Factory class |
72 |
| - @PostConstruct |
73 |
| - public void initialize() { |
74 |
| - initializeTracing(); |
75 |
| - initializeCassandraSession(); |
76 | 34 | }
|
77 | 35 |
|
78 |
| - @PreDestroy |
79 |
| - public void cleanup() { |
80 |
| - if (this.session != null) { |
81 |
| - logger.info("closing CsqlSession"); |
82 |
| - this.session.close(); |
83 |
| - } |
84 |
| - } |
85 |
| - |
86 |
| - private void initializeCassandraSession() { |
87 |
| - CqlSession session; |
| 36 | + private void retrieveAccountFromDB( |
| 37 | + String accountId, StreamObserver<CasaAccount> responseObserver) { |
| 38 | + logger.info("Retrieving CasaAccount details for {}", accountId); |
88 | 39 |
|
89 |
| - if ("astra".equalsIgnoreCase(cassandraInstance)) { |
90 |
| - session = |
91 |
| - CqlSession.builder() |
92 |
| - .withCloudSecureConnectBundle(Paths.get("secure-connect-vino9.zip")) |
93 |
| - .withAuthCredentials(dbUsername, dbPassword) |
94 |
| - .withKeyspace("vino9") |
95 |
| - .build(); |
96 |
| - } else { |
97 |
| - session = |
98 |
| - CqlSession.builder() |
99 |
| - .addContactPoint(new InetSocketAddress("127.0.0.1", 9042)) |
100 |
| - .withKeyspace("bank") |
101 |
| - .withLocalDatacenter("Cassandra") |
102 |
| - .build(); |
103 |
| - } |
| 40 | + ResultSet rs = |
| 41 | + session.execute( |
| 42 | + SimpleStatement.builder("SELECT * FROM casa_account WHERE account_id = ?") |
| 43 | + .addPositionalValue(accountId) |
| 44 | + .build()); |
104 | 45 |
|
105 |
| - ResultSet rs = session.execute("select release_version from system.local"); |
106 | 46 | Row row = rs.one();
|
107 |
| - String releaseVersion = row.getString("release_version"); |
108 |
| - |
109 |
| - this.session = session; |
110 |
| - |
111 |
| - logger.info("connected to cassandra version {}", releaseVersion); |
112 |
| - } |
113 |
| - |
114 |
| - private void initializeTracing() { |
115 |
| - boolean exporterInitialized = false; |
116 |
| - |
117 |
| - exporterInitialized = initializeStackdriverExporter(); |
118 |
| - |
119 |
| - if (!exporterInitialized) { |
120 |
| - exporterInitialized = initializeJaegerExporter(); |
121 |
| - } |
122 |
| - |
123 |
| - if (!exporterInitialized) { |
124 |
| - logger.info("no exporter available, tracing not initialized"); |
| 47 | + if (row == null) { |
| 48 | + responseObserver.onError( |
| 49 | + Status.INVALID_ARGUMENT.withDescription("casa account not found").asException()); |
125 | 50 | return;
|
126 | 51 | }
|
127 | 52 |
|
128 |
| - TraceConfig traceConfig = Tracing.getTraceConfig(); |
129 |
| - traceConfig.updateActiveTraceParams( |
130 |
| - traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build()); |
131 |
| - } |
132 |
| - |
133 |
| - boolean initializeJaegerExporter() { |
134 |
| - if (jaegerThriftEndpoint != null && jaegerThriftEndpoint.startsWith("http")) { |
135 |
| - JaegerTraceExporter.createAndRegister( |
136 |
| - JaegerExporterConfiguration.builder() |
137 |
| - .setServiceName(appName) |
138 |
| - .setThriftEndpoint(jaegerThriftEndpoint) |
139 |
| - .build()); |
140 |
| - |
141 |
| - logger.info("jaeger exporter initialized"); |
| 53 | + Set<Balance> balances = |
| 54 | + row.getSet("balances", UdtValue.class).stream() |
| 55 | + .map( |
| 56 | + bal -> |
| 57 | + Balance.newBuilder() |
| 58 | + .setAmount(bal.getFloat("amount")) |
| 59 | + .setCreditFlag(bal.getBoolean("credit")) |
| 60 | + .setType(Balance.Type.forNumber(bal.getShort("type"))) |
| 61 | + .build()) |
| 62 | + .collect(toSet()); |
142 | 63 |
|
143 |
| - return true; |
144 |
| - } |
145 |
| - |
146 |
| - return false; |
147 |
| - } |
148 |
| - |
149 |
| - boolean isStackdriverEnabled() { |
150 |
| - return "yes".equalsIgnoreCase(stackdriverFlag) || "true".equalsIgnoreCase(stackdriverFlag); |
151 |
| - } |
152 |
| - |
153 |
| - boolean initializeStackdriverExporter() { |
154 |
| - if (!isStackdriverEnabled()) { |
155 |
| - return false; |
156 |
| - } |
157 |
| - |
158 |
| - Map<String, AttributeValue> attributes = |
159 |
| - Map.of( |
160 |
| - "service", AttributeValue.stringAttributeValue("casa-account-v1"), |
161 |
| - "runtime", AttributeValue.stringAttributeValue(System.getProperty("java.version"))); |
162 |
| - |
163 |
| - try { |
164 |
| - StackdriverTraceExporter.createAndRegister( |
165 |
| - StackdriverTraceConfiguration.builder() |
166 |
| - .setProjectId("vino9-276317") |
167 |
| - .setFixedAttributes(attributes) |
168 |
| - .build()); |
169 |
| - } catch (IOException e) { |
170 |
| - logger.warn("unable to initialize stackdriver exporter", e); |
171 |
| - return false; |
172 |
| - } |
| 64 | + CasaAccount account = |
| 65 | + CasaAccount.newBuilder() |
| 66 | + .setAccountId(row.getString("account_id")) |
| 67 | + .setNickname(row.getString("nickname")) |
| 68 | + .setProdCode(row.getString("prod_code")) |
| 69 | + .setProdName(row.getString("prod_name")) |
| 70 | + .setStatus(CasaAccount.Status.forNumber(0)) |
| 71 | + .addAllBalances(balances) |
| 72 | + .build(); |
173 | 73 |
|
174 |
| - return true; |
| 74 | + responseObserver.onNext(account); |
175 | 75 | }
|
176 | 76 | }
|
0 commit comments