forked from Ericsson/ecchronos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate Unique EcChronos ID Ericsson#678
- Generate eccrhonos_Id that should be unique (uuid + host name) - on start up instance, first check nodes_metadata table for ecchronosId, if it already exists, use that one, otherwise generate now one. - So due to persistence of eccronosId, it would be same even instance get reinstalled or restart. - when instance try to acquire nodes then add this newly ecchronosId into nodes_metadata table with datacenter and nodeId. - The new table nodes_metadata schenma, datacenter is partition key and nodeId is clustering colum and ecchrons_id coulum would hold the Id.
- Loading branch information
sajid riaz
committed
Sep 19, 2024
1 parent
b5178cc
commit 522fa4f
Showing
5 changed files
with
323 additions
and
75 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
134 changes: 134 additions & 0 deletions
134
data/src/main/java/com/ericsson/bss/cassandra/ecchronos/data/sync/EccNodeMetadata.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,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 | ||
{ | ||
|
||
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(); | ||
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()); | ||
} | ||
} |
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.