-
Notifications
You must be signed in to change notification settings - Fork 477
/
CTLAggregation.java
171 lines (134 loc) · 6.9 KB
/
CTLAggregation.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
package org.kaaproject.data_migration;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.kaaproject.data_migration.model.Ctl;
import org.kaaproject.data_migration.model.CtlMetaInfo;
import org.kaaproject.data_migration.model.Schema;
import org.kaaproject.data_migration.utils.datadefinition.DataDefinition;
import org.kaaproject.kaa.server.common.core.algorithms.generation.ConfigurationGenerationException;
import org.kaaproject.kaa.server.common.core.algorithms.generation.DefaultRecordGenerationAlgorithm;
import org.kaaproject.kaa.server.common.core.algorithms.generation.DefaultRecordGenerationAlgorithmImpl;
import org.kaaproject.kaa.server.common.core.configuration.RawData;
import org.kaaproject.kaa.server.common.core.configuration.RawDataFactory;
import org.kaaproject.kaa.server.common.core.schema.RawSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Arrays.binarySearch;
public class CTLAggregation {
private Connection connection;
private QueryRunner runner;
private DataDefinition dd;
private static final Logger LOG = LoggerFactory.getLogger(CTLAggregation.class);
private Map<Ctl, List<Schema>> confSchemasToCTL;
private Set<Ctl> ctls;
public CTLAggregation(Connection connection) {
this.connection = connection;
runner = new QueryRunner();
dd = new DataDefinition(connection);
}
public Map<Ctl, List<Schema>> aggregate(List<Schema> schemas) throws SQLException, ConfigurationGenerationException, IOException {
confSchemasToCTL = new HashMap<>();
ctls = new HashSet<>();
Long currentCTLMetaId = runner.query(connection, "select max(id) as max_id from ctl_metainfo", rs -> rs.next() ? rs.getLong("max_id") : null);
Long currentCtlId = runner.query(connection, "select max(id) as max_id from ctl", rs -> rs.next() ? rs.getLong("max_id") : null);
// CTL creation
for (Schema schema : schemas) {
currentCTLMetaId++;
currentCtlId++;
org.apache.avro.Schema schemaBody = new org.apache.avro.Schema.Parser().parse(schema.getSchems());
String fqn = schemaBody.getFullName();
RawSchema rawSchema = new RawSchema(schemaBody.toString());
DefaultRecordGenerationAlgorithm<RawData> algotithm = new DefaultRecordGenerationAlgorithmImpl<>(rawSchema, new RawDataFactory());
String defaultRecord = algotithm.getRootData().getRawData();
Long tenantId = runner.query(connection, "select tenant_id from application where id = " + schema.getAppId(), rs -> rs.next() ? rs.getLong("tenant_id") : null);
Ctl ctl = new Ctl(currentCtlId, new CtlMetaInfo(currentCTLMetaId, fqn, schema.getAppId(), tenantId), defaultRecord);
if (ctls.isEmpty()) {
confSchemasToCTL.put(ctl, new ArrayList<>(asList(schema)));
ctls.add(ctl);
} else {
Ctl ctlToCompare = sameFqn(ctls, ctl);
if (ctlToCompare != null) {
if (bothAppIdNull(ctlToCompare, ctl)) {
if (sameTenant(ctlToCompare, ctl)) {
aggregateSchemas(ctlToCompare, ctl, schema);
} else {
putToMapSchema(ctlToCompare, ctl, schema, "tenant");
}
} else {
if (sameAppId(ctlToCompare, ctl)) {
aggregateSchemas(ctlToCompare, ctl, schema);
} else {
putToMapSchema(ctlToCompare, ctl, schema, "application");
}
}
} else {
ctlToCompare = sameBody(ctls, ctl);
if (ctlToCompare != null) {
LOG.warn("Schemas {} and {} have different fqn but same body {}", ctl.getMetaInfo().getFqn(), ctlToCompare.getMetaInfo().getFqn(), ctl.getDefaultRecord());
}
confSchemasToCTL.put(ctl, new ArrayList<>(asList(schema)));
ctls.add(ctl);
}
}
}
for (Ctl ctl : ctls) {
CtlMetaInfo mi = ctl.getMetaInfo();
Schema s = confSchemasToCTL.get(ctl).get(0);
runner.insert(connection, "insert into ctl_metainfo values(?, ?, ?, ?)", new ScalarHandler<Long>(), mi.getId(), mi.getFqn(), mi.getAppId(), mi.getTenatnId());
runner.insert(connection, "insert into ctl values(?, ?, ?, ?, ?, ?, ?)", new ScalarHandler<Long>(), ctl.getId(), s.getSchems(), s.getCreatedTime(),
s.getCreatedUsername(), ctl.getDefaultRecord(), s.getVersion(), mi.getId());
}
return confSchemasToCTL;
}
private Ctl sameFqn(Set<Ctl> set, Ctl ctl) {
for (Ctl ctl1 : set) {
if (ctl1.getMetaInfo().getFqn().equals(ctl.getMetaInfo().getFqn())) {
return ctl1;
}
}
return null;
}
private Ctl sameBody(Set<Ctl> set, Ctl ctl) {
for (Ctl ctl1 : set) {
if (ctl1.getMetaInfo().getFqn().equals(ctl.getMetaInfo().getFqn())) {
return ctl1;
}
}
return null;
}
private boolean bothAppIdNull(Ctl c1, Ctl c2) {
return c1.getMetaInfo().getAppId() == null && c2.getMetaInfo().getAppId() == null;
}
private boolean sameBody(Ctl c1, Ctl c2) {
return c1.getDefaultRecord().equals(c2.getDefaultRecord());
}
private boolean sameAppId(Ctl c1, Ctl c2) {
return c1.getMetaInfo().getAppId().equals(c2.getMetaInfo().getAppId());
}
private boolean sameTenant(Ctl c1, Ctl c2) {
return c1.getMetaInfo().getTenatnId().equals(c2.getMetaInfo().getTenatnId());
}
///TODO in future add promotion if needed
private void putToMapSchema(Ctl c1, Ctl newCtl, Schema schema, String scope) {
if (!sameBody(c1, newCtl)) {
LOG.warn("Schemas in different %ss' scopes have different bodies {} and {} but the same fqn {}", scope, newCtl.getDefaultRecord(), c1.getDefaultRecord(), newCtl.getMetaInfo().getFqn());
}
confSchemasToCTL.put(newCtl, new ArrayList<>(asList(schema)));
ctls.add(newCtl);
}
private void aggregateSchemas(Ctl c1, Ctl c2, Schema schema) {
if (!sameBody(c1, c2)) {
CtlMetaInfo mi = c1.getMetaInfo();
String message = format("Unable to do migrate due to schemas with same fqn[%s] and scope[appId=%d, tenant=%d] but different bodies", mi.getFqn(), mi.getAppId(), mi.getTenatnId());
throw new MigrationException(message);
}
List<Schema> sc = confSchemasToCTL.get(c1);
sc.add(schema);
}
}