Skip to content

Commit

Permalink
Create a RepairHistory to Store Information #730
Browse files Browse the repository at this point in the history
- Create a RepairHistory to Store Information
  on Repair Operations Performed by ecChronos.
  • Loading branch information
sajid riaz committed Oct 14, 2024
1 parent a377217 commit 2a3f4b2
Show file tree
Hide file tree
Showing 7 changed files with 631 additions and 0 deletions.
5 changes: 5 additions & 0 deletions application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
<version>1.64</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* 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.application.config.repair;

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);
}
}
}
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.application.config.repair;

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;
}
}
Loading

0 comments on commit 2a3f4b2

Please sign in to comment.