|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.cdm.cql.statement; |
| 17 | + |
| 18 | +import java.math.BigInteger; |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | + |
| 23 | +import com.datastax.cdm.feature.TrackRun; |
| 24 | +import com.datastax.cdm.feature.TrackRun.RUN_TYPE; |
| 25 | +import com.datastax.cdm.job.SplitPartitions; |
| 26 | +import com.datastax.cdm.job.SplitPartitions.Partition; |
| 27 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 28 | +import com.datastax.oss.driver.api.core.cql.BoundStatement; |
| 29 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 30 | + |
| 31 | +public class TargetUpsertRunDetailsStatement { |
| 32 | + private CqlSession session; |
| 33 | + private String keyspaceName; |
| 34 | + private String tableName; |
| 35 | + private long runId; |
| 36 | + private long prevRunId; |
| 37 | + private BoundStatement boundInitInfoStatement; |
| 38 | + private BoundStatement boundInitStatement; |
| 39 | + private BoundStatement boundUpdateInfoStatement; |
| 40 | + private BoundStatement boundUpdateStatement; |
| 41 | + private BoundStatement boundUpdateStartStatement; |
| 42 | + private BoundStatement boundSelectStatement; |
| 43 | + |
| 44 | + public TargetUpsertRunDetailsStatement(CqlSession session, String keyspaceTable) { |
| 45 | + this.session = session; |
| 46 | + String[] ksTab = keyspaceTable.split("\\."); |
| 47 | + this.keyspaceName = ksTab[0]; |
| 48 | + this.tableName = ksTab[1]; |
| 49 | + String cdmKsTabInfo = this.keyspaceName + ".cdm_run_info"; |
| 50 | + String cdmKsTabDetails = this.keyspaceName + ".cdm_run_details"; |
| 51 | + |
| 52 | + this.session.execute("create table if not exists " + cdmKsTabInfo |
| 53 | + + " (table_name text, run_id bigint, run_type text, prev_run_id bigint, start_time timestamp, end_time timestamp, run_info text, primary key (table_name, run_id))"); |
| 54 | + this.session.execute("create table if not exists " + cdmKsTabDetails |
| 55 | + + " (table_name text, run_id bigint, start_time timestamp, token_min bigint, token_max bigint, status text, primary key ((table_name, run_id), token_min))"); |
| 56 | + |
| 57 | + boundInitInfoStatement = bindStatement("INSERT INTO " + cdmKsTabInfo |
| 58 | + + " (table_name, run_id, run_type, prev_run_id, start_time) VALUES (?, ?, ?, ?, dateof(now()))"); |
| 59 | + boundInitStatement = bindStatement("INSERT INTO " + cdmKsTabDetails |
| 60 | + + " (table_name, run_id, token_min, token_max, status) VALUES (?, ?, ?, ?, ?)"); |
| 61 | + boundUpdateInfoStatement = bindStatement("UPDATE " + cdmKsTabInfo |
| 62 | + + " SET end_time = dateof(now()), run_info = ? WHERE table_name = ? AND run_id = ?"); |
| 63 | + boundUpdateStatement = bindStatement( |
| 64 | + "UPDATE " + cdmKsTabDetails + " SET status = ? WHERE table_name = ? AND run_id = ? AND token_min = ?"); |
| 65 | + boundUpdateStartStatement = bindStatement("UPDATE " + cdmKsTabDetails |
| 66 | + + " SET start_time = dateof(now()), status = ? WHERE table_name = ? AND run_id = ? AND token_min = ?"); |
| 67 | + boundSelectStatement = bindStatement("SELECT token_min, token_max FROM " + cdmKsTabDetails |
| 68 | + + " WHERE table_name = ? AND run_id = ? and status in ('NOT_STARTED', 'STARTED', 'FAIL', 'DIFF') ALLOW FILTERING"); |
| 69 | + } |
| 70 | + |
| 71 | + public Collection<SplitPartitions.Partition> getPendingPartitions(long prevRunId) { |
| 72 | + this.prevRunId = prevRunId; |
| 73 | + if (prevRunId == 0) { |
| 74 | + return new ArrayList<SplitPartitions.Partition>(); |
| 75 | + } |
| 76 | + |
| 77 | + final Collection<SplitPartitions.Partition> pendingParts = new ArrayList<SplitPartitions.Partition>(); |
| 78 | + ResultSet rs = session |
| 79 | + .execute(boundSelectStatement.setString("table_name", tableName).setLong("run_id", prevRunId)); |
| 80 | + rs.forEach(row -> { |
| 81 | + Partition part = new Partition(BigInteger.valueOf(row.getLong("token_min")), |
| 82 | + BigInteger.valueOf(row.getLong("token_max"))); |
| 83 | + pendingParts.add(part); |
| 84 | + }); |
| 85 | + |
| 86 | + return pendingParts; |
| 87 | + } |
| 88 | + |
| 89 | + public long initCdmRun(Collection<SplitPartitions.Partition> parts, RUN_TYPE runType) { |
| 90 | + runId = System.currentTimeMillis(); |
| 91 | + session.execute(boundInitInfoStatement.setString("table_name", tableName).setLong("run_id", runId) |
| 92 | + .setString("run_type", runType.toString()).setLong("prev_run_id", prevRunId)); |
| 93 | + parts.forEach(part -> initCdmRun(part)); |
| 94 | + return runId; |
| 95 | + } |
| 96 | + |
| 97 | + private void initCdmRun(Partition partition) { |
| 98 | + session.execute(boundInitStatement.setString("table_name", tableName).setLong("run_id", runId) |
| 99 | + .setLong("token_min", partition.getMin().longValue()) |
| 100 | + .setLong("token_max", partition.getMax().longValue()) |
| 101 | + .setString("status", TrackRun.RUN_STATUS.NOT_STARTED.toString())); |
| 102 | + } |
| 103 | + |
| 104 | + public void updateCdmRunInfo(String runInfo) { |
| 105 | + session.execute(boundUpdateInfoStatement.setString("table_name", tableName).setLong("run_id", runId) |
| 106 | + .setString("run_info", runInfo)); |
| 107 | + } |
| 108 | + |
| 109 | + public void updateCdmRun(BigInteger min, TrackRun.RUN_STATUS status) { |
| 110 | + if (TrackRun.RUN_STATUS.STARTED.equals(status)) { |
| 111 | + session.execute(boundUpdateStartStatement.setString("table_name", tableName).setLong("run_id", runId) |
| 112 | + .setLong("token_min", min.longValue()).setString("status", status.toString())); |
| 113 | + } else { |
| 114 | + session.execute(boundUpdateStatement.setString("table_name", tableName).setLong("run_id", runId) |
| 115 | + .setLong("token_min", min.longValue()).setString("status", status.toString())); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private BoundStatement bindStatement(String stmt) { |
| 120 | + if (null == session) |
| 121 | + throw new RuntimeException("Session is not set"); |
| 122 | + return session.prepare(stmt).bind().setTimeout(Duration.ofSeconds(10)); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments