-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sajid riaz
committed
Sep 19, 2024
1 parent
b5178cc
commit 5b72a8e
Showing
4 changed files
with
199 additions
and
74 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
133 changes: 133 additions & 0 deletions
133
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,133 @@ | ||
/* | ||
* 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 void 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); | ||
} | ||
} | ||
|
||
private ResultSet updateNodeStateStatement(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.