-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Startup metric solves feature request #179 #180
Open
cl-a-us
wants to merge
7
commits into
Aiven-Open:master
Choose a base branch
from
viadee:startup_metric
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b8b5880
reduce class fan out complexity from 20 to 19 (20 is upper boundary)
cl-a-us 7025f75
introduce tTask config INITIAL_MESSAGE_COUNT_METRIC_ENABLED_CONFIG
cl-a-us 5213a94
provide StartupMetric as JMX MBean
cl-a-us c1c1bfc
Introduce CountQuerier to count number of rows in a table or number o…
cl-a-us a333471
Introduce StartupMetricUpdater as service in JdbcSourceTask as config…
cl-a-us 16d3686
fix tests by cleaning up jmx beans in @Before
cl-a-us a2ee826
Merge branch 'aiven:master' into startup_metric
cl-a-us File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/java/io/aiven/connect/jdbc/source/CountQuerier.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,79 @@ | ||
/* | ||
* Copyright 2019 Aiven Oy and jdbc-connector-for-apache-kafka project contributors | ||
* Copyright 2018 Confluent Inc. | ||
* | ||
* 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 io.aiven.connect.jdbc.source; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import org.apache.kafka.connect.errors.ConnectException; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
|
||
import io.aiven.connect.jdbc.dialect.DatabaseDialect; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class CountQuerier extends TableQuerier { | ||
private static final Logger log = LoggerFactory.getLogger(CountQuerier.class); | ||
|
||
public CountQuerier(final DatabaseDialect dialect, final QueryMode mode, final String nameOrQuery) { | ||
super(dialect, mode, nameOrQuery, null); | ||
} | ||
|
||
@Override | ||
protected void createPreparedStatement(final Connection db) throws SQLException { | ||
final String queryStr; | ||
switch (mode) { | ||
case TABLE: | ||
queryStr = dialect.expressionBuilder().append("SELECT count(*) FROM ") | ||
.append(tableId).toString(); | ||
break; | ||
case QUERY: | ||
queryStr = dialect.expressionBuilder().append("SELECT count(*) FROM ") | ||
.append("(") | ||
.append(query) | ||
.append(") as count_query") | ||
.toString(); | ||
break; | ||
default: | ||
throw new ConnectException("Unknown mode: " + mode); | ||
} | ||
log.debug("{} prepared SQL query: {}", this, queryStr); | ||
stmt = dialect.createPreparedStatement(db, queryStr); | ||
} | ||
|
||
@Override | ||
protected ResultSet executeQuery() throws SQLException { | ||
return stmt.executeQuery(); | ||
} | ||
|
||
@Override | ||
public SourceRecord extractRecord() throws SQLException { | ||
throw new UnsupportedOperationException("CountQuerier does not support extracting records"); | ||
} | ||
|
||
public Long count() throws SQLException { | ||
final ResultSet resultSet = this.executeQuery(); | ||
if (resultSet.next()) { | ||
return resultSet.getLong(1); | ||
} | ||
return null; | ||
} | ||
|
||
} |
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
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
143 changes: 143 additions & 0 deletions
143
src/main/java/io/aiven/connect/jdbc/source/StartupMetricUpdater.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,143 @@ | ||
/* | ||
* Copyright 2019 Aiven Oy and jdbc-connector-for-apache-kafka project contributors | ||
* | ||
* 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 io.aiven.connect.jdbc.source; | ||
|
||
import javax.management.InstanceAlreadyExistsException; | ||
import javax.management.MBeanRegistrationException; | ||
import javax.management.MBeanServer; | ||
import javax.management.MalformedObjectNameException; | ||
import javax.management.NotCompliantMBeanException; | ||
import javax.management.ObjectName; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.sql.SQLException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.aiven.connect.jdbc.dialect.DatabaseDialect; | ||
import io.aiven.connect.jdbc.util.CachedConnectionProvider; | ||
import io.aiven.connect.jdbc.util.StartupMetric; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static io.aiven.connect.jdbc.source.JdbcSourceTaskConfig.TABLES_CONFIG; | ||
|
||
public class StartupMetricUpdater { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(StartupMetricUpdater.class); | ||
|
||
private final DatabaseDialect dialect; | ||
private final CachedConnectionProvider cachedConnectionProvider; | ||
private final JdbcSourceConnectorConfig config; | ||
|
||
Map<String, StartupMetric> startupMetricByQueryOrTable; | ||
|
||
Map<String, CountQuerier> countQuerierByTableOrQuery; | ||
|
||
public StartupMetricUpdater(final DatabaseDialect dialect, final CachedConnectionProvider cachedConnectionProvider, | ||
final JdbcSourceConnectorConfig config) { | ||
this.dialect = dialect; | ||
this.cachedConnectionProvider = cachedConnectionProvider; | ||
this.config = config; | ||
startupMetricByQueryOrTable = new HashMap<>(); | ||
countQuerierByTableOrQuery = new HashMap<>(); | ||
} | ||
|
||
public void initializeAndExecuteMetric(final Map<String, String> properties, final String query) { | ||
final CountQuerier.QueryMode queryMode = !query.isEmpty() | ||
? CountQuerier.QueryMode.QUERY : CountQuerier.QueryMode.TABLE; | ||
final List<String> tablesOrQuery = queryMode == CountQuerier.QueryMode.QUERY | ||
? Collections.singletonList(query) : config.getList(TABLES_CONFIG); | ||
|
||
final String taskName = properties.get("name"); | ||
|
||
for (final String tableOrQuery : tablesOrQuery) { | ||
getOrCreateStartupMetric(taskName, queryMode, tableOrQuery); | ||
getOrCreateQuryCounter(queryMode, tableOrQuery); | ||
updateMetric(tableOrQuery); | ||
} | ||
} | ||
|
||
private CountQuerier getOrCreateQuryCounter(final CountQuerier.QueryMode queryMode, final String tableOrQuery) { | ||
if (countQuerierByTableOrQuery.containsKey(tableOrQuery)) { | ||
return countQuerierByTableOrQuery.get(tableOrQuery); | ||
} | ||
final CountQuerier countQuerier = new CountQuerier(dialect, queryMode, tableOrQuery); | ||
countQuerierByTableOrQuery.put(tableOrQuery, countQuerier); | ||
return countQuerier; | ||
} | ||
|
||
public Long updateMetric(final String tableOrQuery) { | ||
final StartupMetric metricsProvider = startupMetricByQueryOrTable.get(tableOrQuery); | ||
final CountQuerier countQuerier = countQuerierByTableOrQuery.get(tableOrQuery); | ||
try { | ||
countQuerier.getOrCreatePreparedStatement(cachedConnectionProvider.getConnection()); | ||
final Long counter = countQuerier.count(); | ||
metricsProvider.updateCounter(counter); | ||
log.info("Update StartupMetric for {}. Set Counter to {}", | ||
tableOrQuery.substring(0, Math.min(10, tableOrQuery.length())), counter); | ||
return counter; | ||
} catch (final SQLException e) { | ||
log.error("Exception while querying for number of possible source records", e); | ||
} | ||
return -1L; | ||
} | ||
|
||
private StartupMetric getOrCreateStartupMetric(final String taskName, | ||
final CountQuerier.QueryMode queryMode, final String tableOrQuery) { | ||
if (startupMetricByQueryOrTable.containsKey(tableOrQuery)) { | ||
return startupMetricByQueryOrTable.get(tableOrQuery); | ||
} | ||
|
||
final StartupMetric metricsProvider = new StartupMetric(); | ||
startupMetricByQueryOrTable.put(tableOrQuery, metricsProvider); | ||
|
||
final String objectNameValue = createObjectName(taskName, queryMode, tableOrQuery); | ||
|
||
registerJmxBean(metricsProvider, objectNameValue); | ||
|
||
return metricsProvider; | ||
} | ||
|
||
private void registerJmxBean(final StartupMetric metricsProvider, final String objectNameValue) { | ||
try { | ||
final ObjectName objectName = new ObjectName(objectNameValue); | ||
final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | ||
server.registerMBean(metricsProvider, objectName); | ||
} catch (MalformedObjectNameException | NotCompliantMBeanException | InstanceAlreadyExistsException | ||
| MBeanRegistrationException e) { | ||
log.error(e.getMessage(), e); | ||
} | ||
} | ||
|
||
private String createObjectName(final String taskName, final CountQuerier.QueryMode queryMode, | ||
final String tableOrQuery) { | ||
final String topicName = config.getString(JdbcSourceConnectorConfig.TOPIC_PREFIX_CONFIG); | ||
|
||
final String identifier = "io.aiven.connect.jdbc.initialImportCount"; | ||
String objectNameValue = String.format("%s:task=\"%s\",topic=\"%s\"", identifier, taskName, topicName); | ||
if (queryMode == CountQuerier.QueryMode.TABLE) { | ||
objectNameValue += String.format(",table=\"%s\"", tableOrQuery); | ||
} | ||
|
||
log.info("CounterMetric Name: {}", objectNameValue); | ||
return objectNameValue; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/io/aiven/connect/jdbc/util/StartupMetric.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,49 @@ | ||
/* | ||
* Copyright 2019 Aiven Oy and jdbc-connector-for-apache-kafka project contributors | ||
* Copyright 2016 Confluent Inc. | ||
* | ||
* 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 io.aiven.connect.jdbc.util; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add proper documentation here. |
||
* dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. | ||
*/ | ||
public class StartupMetric implements StartupMetricMBean { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(StartupMetric.class); | ||
|
||
private long counter = -1; | ||
|
||
public StartupMetric() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public Long getCounter() { | ||
log.debug("getCounter: " + counter); | ||
return counter; | ||
} | ||
|
||
public void updateCounter(final Long counter) { | ||
log.info("setCounter: " + counter); | ||
if (counter != null) { | ||
this.counter = counter.intValue(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/io/aiven/connect/jdbc/util/StartupMetricMBean.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,27 @@ | ||
/* | ||
* Copyright 2019 Aiven Oy and jdbc-connector-for-apache-kafka project contributors | ||
* Copyright 2016 Confluent Inc. | ||
* | ||
* 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 io.aiven.connect.jdbc.util; | ||
|
||
public interface StartupMetricMBean { | ||
|
||
/** | ||
* Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et | ||
* dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. | ||
*/ | ||
Long getCounter(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add proper documentation here. What does this class do. Are there any design constraints that future developers should know about, etc.