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.
Create a RepairHistory to Store Information Ericsson#730
- Create a RepairHistory to Store Information on Repair Operations Performed by ecChronos.
- Loading branch information
sajid riaz
committed
Oct 15, 2024
1 parent
a377217
commit aadda11
Showing
6 changed files
with
569 additions
and
0 deletions.
There are no files selected for viewing
213 changes: 213 additions & 0 deletions
213
.../main/java/com/ericsson/bss/cassandra/ecchronos/data/repairhistory/RepairHistoryData.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,213 @@ | ||
/* | ||
* 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.repairhistory; | ||
|
||
import com.ericsson.bss.cassandra.ecchronos.utils.enums.repair.RepairHistoryStatus; | ||
import com.google.common.base.Preconditions; | ||
import java.time.Instant; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
public final class RepairHistoryData | ||
{ | ||
private final UUID myTableId; | ||
private final UUID myNodeId; | ||
private final UUID myRepairId; | ||
private final UUID myJobId; | ||
private final UUID myCoordinatorId; | ||
private final String myRangeBegin; | ||
private final String myRangeEnd; | ||
private final Set<UUID> myParticipants; | ||
private final RepairHistoryStatus myStatus; | ||
private final Instant myStartedAt; | ||
private final Instant myFinishedAt; | ||
|
||
private RepairHistoryData(final Builder builder) | ||
{ | ||
this.myTableId = builder.myTableId; | ||
this.myNodeId = builder.myNodeId; | ||
this.myRepairId = builder.myRepairId; | ||
this.myJobId = builder.myJobId; | ||
this.myCoordinatorId = builder.myCoordinatorId; | ||
this.myRangeBegin = builder.myRangeBegin; | ||
this.myRangeEnd = builder.myRangeEnd; | ||
this.myParticipants = builder.myParticipants == null ? Set.of() : Set.copyOf(builder.myParticipants); | ||
this.myStatus = builder.myStatus; | ||
this.myStartedAt = builder.myStartedAt; | ||
this.myFinishedAt = builder.myFinishedAt; | ||
} | ||
|
||
public UUID getTableId() | ||
{ | ||
return myTableId; | ||
} | ||
|
||
public UUID getNodeId() | ||
{ | ||
return myNodeId; | ||
} | ||
|
||
public UUID getJobId() | ||
{ | ||
return myJobId; | ||
} | ||
|
||
public UUID getRepairId() | ||
{ | ||
return myRepairId; | ||
} | ||
|
||
public UUID getCoordinatorId() | ||
{ | ||
return myCoordinatorId; | ||
} | ||
|
||
public String getRangeBegin() | ||
{ | ||
return myRangeBegin; | ||
} | ||
|
||
public String getRangeEnd() | ||
{ | ||
return myRangeEnd; | ||
} | ||
|
||
public Set<UUID> getParticipants() | ||
{ | ||
return myParticipants; | ||
} | ||
|
||
public RepairHistoryStatus getStatus() | ||
{ | ||
return myStatus; | ||
} | ||
|
||
public Instant getStartedAt() | ||
{ | ||
return myStartedAt; | ||
} | ||
|
||
public Instant getFinishedAt() | ||
{ | ||
return myFinishedAt; | ||
} | ||
|
||
public static Builder copyOf(final RepairHistoryData repairHistoryData) | ||
{ | ||
return new RepairHistoryData.Builder() | ||
.withTableId(repairHistoryData.myTableId) | ||
.withNodeId(repairHistoryData.myNodeId) | ||
.withRepairId(repairHistoryData.myRepairId) | ||
.withJobId(repairHistoryData.myJobId) | ||
.withCoordinatorId(repairHistoryData.myCoordinatorId) | ||
.withRangeBegin(repairHistoryData.myRangeBegin) | ||
.withRangeEnd(repairHistoryData.myRangeEnd) | ||
.withParticipants(repairHistoryData.myParticipants) | ||
.withStatus(repairHistoryData.myStatus) | ||
.withStartedAt(repairHistoryData.myStartedAt) | ||
.withFinishedAt(repairHistoryData.myFinishedAt); | ||
} | ||
|
||
public static final class Builder | ||
{ | ||
private UUID myTableId; | ||
private UUID myNodeId; | ||
private UUID myRepairId; | ||
private UUID myJobId; | ||
private UUID myCoordinatorId; | ||
private String myRangeBegin; | ||
private String myRangeEnd; | ||
private Set<UUID> myParticipants; | ||
private RepairHistoryStatus myStatus; | ||
private Instant myStartedAt; | ||
private Instant myFinishedAt; | ||
|
||
public Builder withTableId(final UUID tableId) | ||
{ | ||
this.myTableId = tableId; | ||
return this; | ||
} | ||
|
||
public Builder withNodeId(final UUID nodeId) | ||
{ | ||
this.myNodeId = nodeId; | ||
return this; | ||
} | ||
|
||
public Builder withRepairId(final UUID repairId) | ||
{ | ||
this.myRepairId = repairId; | ||
return this; | ||
} | ||
|
||
public Builder withJobId(final UUID jobId) | ||
{ | ||
this.myJobId = jobId; | ||
return this; | ||
} | ||
|
||
public Builder withCoordinatorId(final UUID coordinatorId) | ||
{ | ||
this.myCoordinatorId = coordinatorId; | ||
return this; | ||
} | ||
|
||
public Builder withRangeBegin(final String rangeBegin) | ||
{ | ||
this.myRangeBegin = rangeBegin; | ||
return this; | ||
} | ||
|
||
public Builder withRangeEnd(final String rangeEnd) | ||
{ | ||
this.myRangeEnd = rangeEnd; | ||
return this; | ||
} | ||
|
||
public Builder withParticipants(final Set<UUID> participants) | ||
{ | ||
this.myParticipants = (participants == null) ? Set.of() : new HashSet<>(participants); | ||
return this; | ||
} | ||
|
||
public Builder withStatus(final RepairHistoryStatus status) | ||
{ | ||
this.myStatus = status; | ||
return this; | ||
} | ||
|
||
public Builder withStartedAt(final Instant startedAt) | ||
{ | ||
this.myStartedAt = startedAt; | ||
return this; | ||
} | ||
|
||
public Builder withFinishedAt(final Instant finishedAt) | ||
{ | ||
this.myFinishedAt = finishedAt; | ||
return this; | ||
} | ||
|
||
public RepairHistoryData build() | ||
{ | ||
Preconditions.checkNotNull(myTableId, "Table ID cannot be null"); | ||
Preconditions.checkNotNull(myNodeId, "Node ID cannot be null"); | ||
Preconditions.checkNotNull(myRepairId, "Repair ID cannot be null"); | ||
Preconditions.checkNotNull(myStatus, "Status cannot be null"); | ||
return new RepairHistoryData(this); | ||
} | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
...in/java/com/ericsson/bss/cassandra/ecchronos/data/repairhistory/RepairHistoryService.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,166 @@ | ||
/* | ||
* 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.repairhistory; | ||
|
||
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.querybuilder.QueryBuilder; | ||
import com.google.common.base.Preconditions; | ||
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 RepairHistoryService | ||
{ | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RepairHistoryService.class); | ||
|
||
private static final String KEYSPACE_NAME = "ecchronos"; | ||
private static final String TABLE_NAME = "repair_history"; | ||
private static final String COLUMN_NODE_ID = "node_id"; | ||
private static final String COLUMN_TABLE_ID = "table_id"; | ||
private static final String COLUMN_REPAIR_ID = "repair_id"; | ||
private static final String COLUMN_JOB_ID = "job_id"; | ||
private static final String COLUMN_COORDINATOR_ID = "coordinator_id"; | ||
private static final String COLUMN_RANGE_BEGIN = "range_begin"; | ||
private static final String COLUMN_RANGE_END = "range_end"; | ||
private static final String COLUMN_PARTICIPANTS = "participants"; | ||
private static final String COLUMN_STATUS = "status"; | ||
private static final String COLUMN_STARTED_AT = "started_at"; | ||
private static final String COLUMN_FINISHED_AT = "finished_at"; | ||
|
||
private final PreparedStatement myCreateStatement; | ||
private final PreparedStatement myUpdateStatement; | ||
private final PreparedStatement mySelectStatement; | ||
private final CqlSession myCqlSession; | ||
|
||
public RepairHistoryService(final CqlSession cqlSession) | ||
{ | ||
myCqlSession = Preconditions.checkNotNull(cqlSession, "CqlSession cannot be null"); | ||
myCreateStatement = myCqlSession | ||
.prepare(QueryBuilder.insertInto(KEYSPACE_NAME, TABLE_NAME) | ||
.value(COLUMN_TABLE_ID, bindMarker()) | ||
.value(COLUMN_NODE_ID, bindMarker()) | ||
.value(COLUMN_REPAIR_ID, bindMarker()) | ||
.value(COLUMN_JOB_ID, bindMarker()) | ||
.value(COLUMN_COORDINATOR_ID, bindMarker()) | ||
.value(COLUMN_RANGE_BEGIN, bindMarker()) | ||
.value(COLUMN_RANGE_END, bindMarker()) | ||
.value(COLUMN_PARTICIPANTS, bindMarker()) | ||
.value(COLUMN_STATUS, bindMarker()) | ||
.value(COLUMN_STARTED_AT, bindMarker()) | ||
.value(COLUMN_FINISHED_AT, bindMarker()) | ||
.build() | ||
.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)); | ||
myUpdateStatement = myCqlSession | ||
.prepare(QueryBuilder.update(KEYSPACE_NAME, TABLE_NAME) | ||
.setColumn(COLUMN_JOB_ID, bindMarker()) | ||
.setColumn(COLUMN_COORDINATOR_ID, bindMarker()) | ||
.setColumn(COLUMN_RANGE_BEGIN, bindMarker()) | ||
.setColumn(COLUMN_RANGE_END, bindMarker()) | ||
.setColumn(COLUMN_PARTICIPANTS, bindMarker()) | ||
.setColumn(COLUMN_STATUS, bindMarker()) | ||
.setColumn(COLUMN_STARTED_AT, bindMarker()) | ||
.setColumn(COLUMN_FINISHED_AT, bindMarker()) | ||
.whereColumn(COLUMN_TABLE_ID) | ||
.isEqualTo(bindMarker()) | ||
.whereColumn(COLUMN_NODE_ID) | ||
.isEqualTo(bindMarker()) | ||
.whereColumn(COLUMN_REPAIR_ID) | ||
.isEqualTo(bindMarker()) | ||
.build() | ||
.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)); | ||
mySelectStatement = myCqlSession | ||
.prepare(selectFrom(KEYSPACE_NAME, TABLE_NAME) | ||
.columns(COLUMN_NODE_ID, COLUMN_TABLE_ID, COLUMN_REPAIR_ID, COLUMN_JOB_ID, COLUMN_COORDINATOR_ID, | ||
COLUMN_RANGE_BEGIN, COLUMN_RANGE_END, COLUMN_PARTICIPANTS, COLUMN_STATUS, COLUMN_STARTED_AT, | ||
COLUMN_FINISHED_AT) | ||
.whereColumn(COLUMN_TABLE_ID) | ||
.isEqualTo(bindMarker()) | ||
.whereColumn(COLUMN_NODE_ID) | ||
.isEqualTo(bindMarker()) | ||
.build() | ||
.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)); | ||
} | ||
|
||
public ResultSet getRepairHistoryInfo(final RepairHistoryData repairHistoryData) | ||
{ | ||
BoundStatement boundStatement = mySelectStatement.bind(repairHistoryData.getTableId(), | ||
repairHistoryData.getNodeId()); | ||
return myCqlSession.execute(boundStatement); | ||
} | ||
|
||
public ResultSet insertRepairHistoryInfo(final RepairHistoryData repairHistoryData) | ||
{ | ||
LOG.info("Preparing to insert repair history with tableId {} and nodeId {}", | ||
repairHistoryData.getTableId(), repairHistoryData.getNodeId()); | ||
BoundStatement repairHistoryInfo = myCreateStatement.bind(repairHistoryData.getTableId(), | ||
repairHistoryData.getNodeId(), | ||
repairHistoryData.getRepairId(), | ||
repairHistoryData.getJobId(), | ||
repairHistoryData.getCoordinatorId(), | ||
repairHistoryData.getRangeBegin(), | ||
repairHistoryData.getRangeEnd(), | ||
repairHistoryData.getParticipants(), | ||
repairHistoryData.getStatus().name(), | ||
repairHistoryData.getStartedAt(), | ||
repairHistoryData.getFinishedAt()); | ||
ResultSet tmpResultSet = myCqlSession.execute(repairHistoryInfo); | ||
if (tmpResultSet.wasApplied()) | ||
{ | ||
LOG.info("RepairHistory inserted successfully with tableId {} and nodeId {}", | ||
repairHistoryData.getTableId(), repairHistoryData.getNodeId()); | ||
} | ||
else | ||
{ | ||
LOG.error("Unable to insert repairHistory with tableId {} and nodeId {}", | ||
repairHistoryData.getTableId(), | ||
repairHistoryData.getNodeId()); | ||
} | ||
return tmpResultSet; | ||
} | ||
|
||
public ResultSet updateRepairHistoryInfo(final RepairHistoryData repairHistoryData) | ||
{ | ||
BoundStatement updateRepairHistoryInfo = myUpdateStatement.bind(repairHistoryData.getJobId(), | ||
repairHistoryData.getCoordinatorId(), | ||
repairHistoryData.getRangeBegin(), | ||
repairHistoryData.getRangeEnd(), | ||
repairHistoryData.getParticipants(), | ||
repairHistoryData.getStatus().name(), | ||
repairHistoryData.getStartedAt(), | ||
repairHistoryData.getFinishedAt(), | ||
repairHistoryData.getTableId(), | ||
repairHistoryData.getNodeId(), | ||
repairHistoryData.getRepairId()); | ||
ResultSet tmpResultSet = myCqlSession.execute(updateRepairHistoryInfo); | ||
if (tmpResultSet.wasApplied()) | ||
{ | ||
LOG.info("RepairHistory successfully updated for tableId {} and nodeId {}", | ||
repairHistoryData.getTableId(), repairHistoryData.getNodeId()); | ||
} | ||
else | ||
{ | ||
LOG.error("RepairHistory updated failed for tableId {} and nodeId {}", | ||
repairHistoryData.getTableId(), repairHistoryData.getNodeId()); | ||
} | ||
return tmpResultSet; | ||
} | ||
} |
Oops, something went wrong.