Refactor register API for making integration easier. (#2075)
* Refactor register API for making integration easier. * Fix wrong refactor.
This commit is contained in:
parent
d789775e17
commit
c592aea8db
|
|
@ -78,8 +78,8 @@ public class RegisterPersistentWorker extends AbstractWorker<RegisterSource> {
|
|||
dbSource.combine(source);
|
||||
registerDAO.forceUpdate(modelName, dbSource);
|
||||
} else {
|
||||
int sequence = registerDAO.max(modelName);
|
||||
source.setSequence(sequence + 1);
|
||||
int sequence = registerDAO.registerId(modelName, source);
|
||||
source.setSequence(sequence);
|
||||
registerDAO.forceInsert(modelName, source);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,14 @@ import org.apache.skywalking.oap.server.core.register.RegisterSource;
|
|||
*/
|
||||
public interface IRegisterDAO extends DAO {
|
||||
|
||||
int max(String modelName) throws IOException;
|
||||
/**
|
||||
* According modelName and register source, try to get the unique ID for this particular model.
|
||||
* @param modelName
|
||||
* @param registerSource
|
||||
* @return the unique id. This ID for each model should start with 2. 1 has been reserved.
|
||||
* @throws IOException
|
||||
*/
|
||||
int registerId(String modelName, RegisterSource registerSource) throws IOException;
|
||||
|
||||
RegisterSource get(String modelName, String id) throws IOException;
|
||||
|
||||
|
|
|
|||
|
|
@ -78,7 +78,8 @@ public class RegisterEsDAO extends EsDAO implements IRegisterDAO {
|
|||
getClient().forceUpdate(modelName, source.id(), builder);
|
||||
}
|
||||
|
||||
@Override public int max(String modelName) throws IOException {
|
||||
@Override public int registerId(String modelName,
|
||||
RegisterSource registerSource) throws IOException {
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||
searchSourceBuilder.aggregation(AggregationBuilders.max(RegisterSource.SEQUENCE).field(RegisterSource.SEQUENCE));
|
||||
searchSourceBuilder.size(0);
|
||||
|
|
@ -91,9 +92,9 @@ public class RegisterEsDAO extends EsDAO implements IRegisterDAO {
|
|||
|
||||
int id = (int)agg.getValue();
|
||||
if (id == Integer.MAX_VALUE || id == Integer.MIN_VALUE) {
|
||||
return 1;
|
||||
return 2;
|
||||
} else {
|
||||
return id;
|
||||
return id + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,15 +46,16 @@ public class H2RegisterDAO extends H2SQLExecutor implements IRegisterDAO {
|
|||
this.storageBuilder = storageBuilder;
|
||||
}
|
||||
|
||||
@Override public int max(String modelName) throws IOException {
|
||||
@Override public int registerId(String modelName,
|
||||
RegisterSource registerSource) throws IOException {
|
||||
try (Connection connection = h2Client.getConnection()) {
|
||||
try (ResultSet rs = h2Client.executeQuery(connection, "SELECT max(sequence) max_id FROM " + modelName)) {
|
||||
while (rs.next()) {
|
||||
int maxId = rs.getInt("max_id");
|
||||
if (maxId == 0) {
|
||||
return 1;
|
||||
return 2;
|
||||
} else {
|
||||
return maxId;
|
||||
return maxId + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue