Commit d5658d5b32bbfeebad90fc0b7519865d258ad280

Authored by AndrewVolosytnykhThingsboard
Committed by Andrew Shvayka
1 parent 1240099c

Code cleaning, improvement of README.md

... ... @@ -54,7 +54,8 @@
54 54 <cassandra.version>4.10.0</cassandra.version>
55 55 <metrics.version>4.0.5</metrics.version>
56 56 <cassandra-unit.version>4.3.1.0</cassandra-unit.version>
57   - <cassandra-all.version>3.11.9</cassandra-all.version>
  57 + <cassandra-all.version>3.11.10</cassandra-all.version>
  58 + <cassandra-driver-core.version>3.11.0</cassandra-driver-core.version>
58 59 <takari-cpsuite.version>1.2.7</takari-cpsuite.version>
59 60 <guava.version>28.2-jre</guava.version>
60 61 <caffeine.version>2.6.1</caffeine.version>
... ... @@ -1096,6 +1097,11 @@
1096 1097 <version>${cassandra.version}</version>
1097 1098 </dependency>
1098 1099 <dependency>
  1100 + <groupId>com.datastax.cassandra</groupId>
  1101 + <artifactId>cassandra-driver-core</artifactId>
  1102 + <version>${cassandra-driver-core.version}</version>
  1103 + </dependency>
  1104 + <dependency>
1099 1105 <groupId>io.dropwizard.metrics</groupId>
1100 1106 <artifactId>metrics-jmx</artifactId>
1101 1107 <version>${metrics.version}</version>
... ...
... ... @@ -54,17 +54,14 @@
54 54 <dependency>
55 55 <groupId>org.apache.cassandra</groupId>
56 56 <artifactId>cassandra-all</artifactId>
57   - <version>3.11.10</version>
58 57 </dependency>
59 58 <dependency>
60 59 <groupId>com.datastax.cassandra</groupId>
61 60 <artifactId>cassandra-driver-core</artifactId>
62   - <version>3.10.1</version>
63 61 </dependency>
64 62 <dependency>
65 63 <groupId>commons-io</groupId>
66 64 <artifactId>commons-io</artifactId>
67   - <version>2.5</version>
68 65 </dependency>
69 66 </dependencies>
70 67
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  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 + */
1 16 package org.thingsboard.client.tools.migrator;
2 17
3 18 import com.google.common.collect.Lists;
... ... @@ -122,29 +137,52 @@ public class PgCaMigrator {
122 137 }
123 138 }
124 139
125   - private void logLinesProcessed() {
126   - if (linesProcessed++ % LOG_BATCH == 0) {
127   - System.out.println(new Date() + " linesProcessed = " + linesProcessed + " in, castOk " + castedOk + " castErr " + castErrors);
  140 + private void logLinesProcessed(long lines) {
  141 + if (lines % LOG_BATCH == 0) {
  142 + System.out.println(new Date() + " lines processed = " + lines + " in, castOk " + castedOk + " castErr " + castErrors);
128 143 }
129 144 }
130 145
131   - private List<Object> toValuesTs(List<String> raw) {
132   - linesTsMigrated++;
133   - List<Object> result = new ArrayList<>();
  146 + private void logLinesMigrated(long lines) {
  147 + if(lines % LOG_BATCH == 0) {
  148 + System.out.println(new Date() + " lines migrated = " + lines + " in, castOk " + castedOk + " castErr " + castErrors);
  149 + }
  150 + }
  151 +
  152 + private void addTypeIdKey(List<Object> result, List<String> raw) {
134 153 result.add(entityIdsAndTypes.getEntityType(raw.get(0)));
135 154 result.add(UUID.fromString(raw.get(0)));
136 155 result.add(keyParser.getKeyByKeyId(raw.get(1)));
  156 + }
137 157
  158 + private void addPartitions(List<Object> result, List<String> raw) {
138 159 long ts = Long.parseLong(raw.get(2));
139 160 long partition = toPartitionTs(ts);
140 161 result.add(partition);
141 162 result.add(ts);
  163 + }
  164 +
  165 + private void addTimeseries(List<Object> result, List<String> raw) {
  166 + result.add(Long.parseLong(raw.get(2)));
  167 + }
142 168
  169 + private void addValues(List<Object> result, List<String> raw) {
143 170 result.add(raw.get(3).equals("\\N") ? null : raw.get(3).equals("t") ? Boolean.TRUE : Boolean.FALSE);
144 171 result.add(raw.get(4).equals("\\N") ? null : raw.get(4));
145 172 result.add(raw.get(5).equals("\\N") ? null : Long.parseLong(raw.get(5)));
146 173 result.add(raw.get(6).equals("\\N") ? null : Double.parseDouble(raw.get(6)));
147 174 result.add(raw.get(7).equals("\\N") ? null : raw.get(7));
  175 + }
  176 +
  177 + private List<Object> toValuesTs(List<String> raw) {
  178 +
  179 + logLinesMigrated(linesTsMigrated++);
  180 +
  181 + List<Object> result = new ArrayList<>();
  182 +
  183 + addTypeIdKey(result, raw);
  184 + addPartitions(result, raw);
  185 + addValues(result, raw);
148 186
149 187 processPartitions(result);
150 188
... ... @@ -152,20 +190,12 @@ public class PgCaMigrator {
152 190 }
153 191
154 192 private List<Object> toValuesLatest(List<String> raw) {
155   - linesLatestMigrated++;
  193 + logLinesMigrated(linesLatestMigrated++);
156 194 List<Object> result = new ArrayList<>();
157   - result.add(this.entityIdsAndTypes.getEntityType(raw.get(0)));
158   - result.add(UUID.fromString(raw.get(0)));
159   - result.add(this.keyParser.getKeyByKeyId(raw.get(1)));
160   -
161   - long ts = Long.parseLong(raw.get(2));
162   - result.add(3, ts);
163 195
164   - result.add(raw.get(3).equals("\\N") ? null : raw.get(3).equals("t") ? Boolean.TRUE : Boolean.FALSE);
165   - result.add(raw.get(4).equals("\\N") ? null : raw.get(4));
166   - result.add(raw.get(5).equals("\\N") ? null : Long.parseLong(raw.get(5)));
167   - result.add(raw.get(6).equals("\\N") ? null : Double.parseDouble(raw.get(6)));
168   - result.add(raw.get(7).equals("\\N") ? null : raw.get(7));
  196 + addTypeIdKey(result, raw);
  197 + addTimeseries(result, raw);
  198 + addValues(result, raw);
169 199
170 200 return result;
171 201 }
... ... @@ -184,7 +214,7 @@ public class PgCaMigrator {
184 214 String currentLine;
185 215 linesProcessed = 0;
186 216 while(iterator.hasNext()) {
187   - logLinesProcessed();
  217 + logLinesProcessed(linesProcessed++);
188 218 currentLine = iterator.nextLine();
189 219 if(isBlockFinished(currentLine)) {
190 220 return;
... ...
... ... @@ -21,8 +21,6 @@ It will generate single jar file with all required dependencies inside `target d
21 21 #### Dump data from the source Postgres Database
22 22 *Do not use compression if possible because Tool can only work with uncompressed file
23 23
24   -*If you want to migrate just `ts_kv` without `ts_kv_latest` just don't dump an unnecessary table and when starting the tool don't use arguments (paths) for input dump and output files*
25   -
26 24 1. Dump related tables that need to correct save telemetry
27 25
28 26 `pg_dump -h localhost -U postgres -d thingsboard -t tenant -t customer -t user -t dashboard -t asset -t device -t alarm -t rule_chain -t rule_node -t entity_view -t widgets_bundle -t widget_type -t tenant_profile -t device_profile -t api_usage_state -t tb_user > related_entities.dmp`
... ... @@ -38,21 +36,26 @@ Tool use 3 different directories for saving SSTables - `ts_kv_cf`, `ts_kv_latest
38 36
39 37 Create 3 empty directories. For example:
40 38
41   - /home/ubunut/migration/ts
42   - /home/ubunut/migration/ts_latest
43   - /home/ubunut/migration/ts_partition
  39 + /home/user/migration/ts
  40 + /home/user/migration/ts_latest
  41 + /home/user/migration/ts_partition
44 42
45 43 #### Run tool
46   -*Note: if you run this tool on remote instance - don't forget to execute this command in `screen` to avoid unexpected termination
  44 +
  45 +**If you want to migrate just `ts_kv` without `ts_kv_latest` or vice versa don't use arguments (paths) for output files*
  46 +
  47 +**Note: if you run this tool on remote instance - don't forget to execute this command in `screen` to avoid unexpected termination*
47 48
48 49 ```
49 50 java -jar ./tools-2.4.1-SNAPSHOT-jar-with-dependencies.jar
50   - -telemetryFrom ./source/ts_kv_all.dmp
  51 + -telemetryFrom /home/user/dump/ts_kv_all.dmp
  52 + -relatedEntities /home/user/dump/relatedEntities.dmp
51 53 -latestOut /home/ubunut/migration/ts_latest
52   - -tsOut /home/ubunut/migration/ts
53   - -partitionsOut /home/ubunut/migration/ts_partition
54   - -castEnable false
  54 + -tsOut /home/ubunut/migration/ts
  55 + -partitionsOut /home/ubunut/migration/ts_partition
  56 + -castEnable false
55 57 ```
  58 +*Use your paths for program arguments*
56 59
57 60 Tool execution time depends on DB size, CPU resources and Disk throughput
58 61
... ... @@ -62,20 +65,21 @@ Tool execution time depends on DB size, CPU resources and Disk throughput
62 65 1. [Optional] install Cassandra on the instance
63 66 2. [Optional] Using `cqlsh` create `thingsboard` keyspace and requred tables from this file `schema-ts.cql`
64 67 3. Stop Cassandra
65   -4. Copy generated SSTable files into cassandra data dir:
  68 +4. Look at `/var/lib/cassandra/data/thingsboard` and check for names of data folders
  69 +5. Copy generated SSTable files into cassandra data dir using next command:
66 70
67 71 ```
68   - sudo find /home/ubunut/migration/ts -name '*.*' -exec mv {} /var/lib/cassandra/data/thingsboard/ts_kv_cf-0e9aaf00ee5511e9a5fa7d6f489ffd13/ \;
69   - sudo find /home/ubunut/migration/ts_latest -name '*.*' -exec mv {} /var/lib/cassandra/data/thingsboard/ts_kv_latest_cf-161449d0ee5511e9a5fa7d6f489ffd13/ \;
70   - sudo find /home/ubunut/migration/ts_partition -name '*.*' -exec mv {} /var/lib/cassandra/data/thingsboard/ts_kv_partitions_cf-12e8fa80ee5511e9a5fa7d6f489ffd13/ \;
  72 + sudo find /home/user/migration/ts -name '*.*' -exec mv {} /var/lib/cassandra/data/thingsboard/ts_kv_cf-0e9aaf00ee5511e9a5fa7d6f489ffd13/ \;
  73 + sudo find /home/user/migration/ts_latest -name '*.*' -exec mv {} /var/lib/cassandra/data/thingsboard/ts_kv_latest_cf-161449d0ee5511e9a5fa7d6f489ffd13/ \;
  74 + sudo find /home/user/migration/ts_partition -name '*.*' -exec mv {} /var/lib/cassandra/data/thingsboard/ts_kv_partitions_cf-12e8fa80ee5511e9a5fa7d6f489ffd13/ \;
71 75 ```
72   -
73   -5. Start Cassandra service and trigger compaction
  76 + *Pay attention! Data folders have similar name `ts_kv_cf-0e9aaf00ee5511e9a5fa7d6f489ffd13`, but you have to use own*
  77 +6. Start Cassandra service and trigger compaction
  78 +
  79 + Trigger compactions: `nodetool compact thingsboard`
  80 +
  81 + Check compaction status: `nodetool compactionstats`
74 82
75   -```
76   - trigger compactions: nodetool compact thingsboard
77   - check compaction status: nodetool compactionstats
78   -```
79 83
80 84 ## Switch Thignsboard into Hybrid Mode
81 85
... ...