-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate Unique EcChronos ID #678 #713
base: agent/master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 2024 Telefonaktiebolaget LM Ericsson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.ericsson.bss.cassandra.ecchronos.data.sync; | ||
|
||
import com.datastax.oss.driver.api.core.ConsistencyLevel; | ||
import com.datastax.oss.driver.api.core.CqlSession; | ||
import com.datastax.oss.driver.api.core.cql.BoundStatement; | ||
import com.datastax.oss.driver.api.core.cql.PreparedStatement; | ||
import com.datastax.oss.driver.api.core.cql.ResultSet; | ||
import com.datastax.oss.driver.api.core.cql.Row; | ||
import com.datastax.oss.driver.api.core.metadata.Node; | ||
import com.datastax.oss.driver.api.querybuilder.QueryBuilder; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker; | ||
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.selectFrom; | ||
|
||
public final class EccNodeMetadata | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EccNodesMetadata |
||
{ | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(EccNodeMetadata.class); | ||
private static final String COLUMN_ECCHRONOS_ID = "ecchronos_id"; | ||
private static final String COLUMN_DC_NAME = "datacenter_name"; | ||
private static final String COLUMN_NODE_ID = "node_id"; | ||
private static final String KEYSPACE_NAME = "ecchronos"; | ||
private static final String NODE_METADATA_TABLE_NAME = "nodes_metadata"; | ||
|
||
private final PreparedStatement mySelectStatement; | ||
private final PreparedStatement myCreateStatement; | ||
private final PreparedStatement myTableUpdateStatement; | ||
private final CqlSession mySession; | ||
private final Node myNode; | ||
|
||
public EccNodeMetadata(final List<Node> nodes, final CqlSession session) | ||
{ | ||
mySession = session; | ||
myNode = nodes.stream().findAny().get(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the topology changes and you get a node that was not on the first configuration for a instance that was working? let's say that in the configuration we are using datacenterAware, so we have a datacenter list of [d1, dc2] but now the topology changed and we added dc3 on the list, but it was not on the previously topology for this instance, so if this line picks up a node from dc3 it will generate a new ecchronos id for a instance that already has an ID |
||
myCreateStatement = mySession.prepare(QueryBuilder.insertInto(KEYSPACE_NAME, NODE_METADATA_TABLE_NAME) | ||
.value(COLUMN_DC_NAME, bindMarker()) | ||
.value(COLUMN_NODE_ID, bindMarker()) | ||
.value(COLUMN_ECCHRONOS_ID, bindMarker()) | ||
.build()); | ||
myTableUpdateStatement = mySession.prepare(QueryBuilder.update(KEYSPACE_NAME, NODE_METADATA_TABLE_NAME) | ||
.setColumn(COLUMN_ECCHRONOS_ID, bindMarker()) | ||
.whereColumn(COLUMN_DC_NAME) | ||
.isEqualTo(bindMarker()) | ||
.whereColumn(COLUMN_NODE_ID) | ||
.isEqualTo(bindMarker()) | ||
.build() | ||
.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)); | ||
mySelectStatement = mySession.prepare(selectFrom(KEYSPACE_NAME, NODE_METADATA_TABLE_NAME) | ||
.columns(COLUMN_ECCHRONOS_ID) | ||
.whereColumn(COLUMN_DC_NAME) | ||
.isEqualTo(bindMarker()) | ||
.build()); | ||
} | ||
|
||
public String getEccChronosID() throws UnknownHostException | ||
{ | ||
ResultSet resultSet = getResultSet(); | ||
|
||
Row row = resultSet.one(); | ||
if (row != null) | ||
{ | ||
return row.getString(COLUMN_ECCHRONOS_ID); | ||
} | ||
return generateNewEcChronosID(); | ||
} | ||
|
||
public ResultSet getResultSet() | ||
{ | ||
BoundStatement boundStatement = mySelectStatement.bind(myNode.getDatacenter()); | ||
return mySession.execute(boundStatement); | ||
} | ||
|
||
public ResultSet insertNodeMetadataInfo(final String datacenterName, | ||
final UUID nodeID, | ||
final String eccChronosID) | ||
{ | ||
BoundStatement insertMetadataInfo = myCreateStatement.bind(datacenterName, nodeID, eccChronosID); | ||
ResultSet tmpResultSet = mySession.execute(insertMetadataInfo); | ||
LOG.info("Preparing to insert node metadata with Datacenter {}, NodeId {} and EccChronosID {}", | ||
datacenterName, nodeID, eccChronosID); | ||
if (tmpResultSet.wasApplied()) | ||
{ | ||
LOG.info("Node metadata info successfully stored with EccChronosID {}", eccChronosID); | ||
} | ||
else | ||
{ | ||
LOG.error("Unable to store metadata info with NodeId{}", nodeID); | ||
} | ||
return tmpResultSet; | ||
} | ||
|
||
public ResultSet updateNodeMetadataEcChronosIdStatement(final String ecChronosId, | ||
final String datacenterName, | ||
final UUID nodeID) | ||
{ | ||
BoundStatement updateEcChronosID = myTableUpdateStatement.bind(ecChronosId, datacenterName, nodeID); | ||
ResultSet tmpResultSet = mySession.execute(updateEcChronosID); | ||
if (tmpResultSet.wasApplied()) | ||
{ | ||
LOG.info("Node metadata {} successfully updated", nodeID); | ||
} | ||
else | ||
{ | ||
LOG.error("Unable to update node metadata {}", nodeID); | ||
} | ||
return tmpResultSet; | ||
} | ||
|
||
private String generateNewEcChronosID() throws UnknownHostException | ||
{ | ||
String uuid = UUID.randomUUID().toString(); | ||
return uuid.concat(InetAddress.getLocalHost().getHostName()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idk about removing it, because we need to generate this ans pass for EccNodeMetadata if the instance is new, I think here we need to have some validation using the EccNodeMetadata, generate the unique id, and them passing for EccNodesSync