(CairoEngine engine, SqlCompiler compiler, SqlExecutionContext sqlExecutionContext)
| 139 | } |
| 140 | |
| 141 | public void init(CairoEngine engine, SqlCompiler compiler, SqlExecutionContext sqlExecutionContext) throws SqlException { |
| 142 | if (!enabled) { |
| 143 | return; |
| 144 | } |
| 145 | String tableName = telemetryType.getTableName(); |
| 146 | boolean shouldDropTable = false; |
| 147 | boolean shouldAlterTtl = false; |
| 148 | TableToken tableToken = null; |
| 149 | try { |
| 150 | tableToken = engine.verifyTableName(tableName); |
| 151 | try (TableMetadata meta = engine.getTableMetadata(tableToken)) { |
| 152 | int ttl = meta.getTtlHoursOrMonths(); |
| 153 | if (ttl == 0) { |
| 154 | shouldDropTable = true; |
| 155 | } else if (ttlWeeks > 0 && ttl > 0 && ttl != ttlWeeks * 24 * 7) { |
| 156 | shouldAlterTtl = true; |
| 157 | } |
| 158 | // Drop and recreate when schema changes (safe for short-TTL telemetry tables). |
| 159 | // Note: uses != rather than <, so a rollback to older code will also re-drop. |
| 160 | int expectedColumnCount = telemetryType.getExpectedColumnCount(); |
| 161 | if (expectedColumnCount > 0 && meta.getColumnCount() != expectedColumnCount) { |
| 162 | shouldDropTable = true; |
| 163 | } |
| 164 | } |
| 165 | } catch (CairoException e) { |
| 166 | if (!Chars.contains(e.getFlyweightMessage(), "table does not exist")) { |
| 167 | throw e; |
| 168 | } |
| 169 | } |
| 170 | if (shouldDropTable) { |
| 171 | compiler.query().$("DROP TABLE '").$(tableName).$("'") |
| 172 | .compile(sqlExecutionContext) |
| 173 | .getOperation() |
| 174 | .execute(sqlExecutionContext, null) |
| 175 | .await(); |
| 176 | } else if (shouldAlterTtl) { |
| 177 | assert tableToken != null; |
| 178 | try (MetadataCacheWriter metadataRW = engine.getMetadataCache().writeLock()) { |
| 179 | metadataRW.hydrateTable(tableToken); |
| 180 | } |
| 181 | compiler.query().$("ALTER TABLE '").$(tableName).$("' SET TTL ").$(ttlWeeks).$(" WEEKS") |
| 182 | .compile(sqlExecutionContext) |
| 183 | .execute(null) |
| 184 | .await(); |
| 185 | } |
| 186 | telemetryType.getCreateSql(compiler.query(), ttlWeeks).createTable(sqlExecutionContext); |
| 187 | tableToken = engine.verifyTableName(tableName); |
| 188 | try { |
| 189 | writer = engine.getWriter(tableToken, "telemetry"); |
| 190 | } catch (CairoException ex) { |
| 191 | LOG.error() |
| 192 | .$("could not open [table=").$(tableToken) |
| 193 | .$(", msg=").$safe(ex.getFlyweightMessage()) |
| 194 | .$(", errno=").$(ex.getErrno()) |
| 195 | .$(']').$(); |
| 196 | } |
| 197 | |
| 198 | telemetryType.logStatus(writer, TelemetryEvent.SYSTEM_UP, clock.getTicks()); |
nothing calls this directly
no test coverage detected