Skip to content

Commit

Permalink
for #153 remove JOB_TRACE_LOG table
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Nov 8, 2016
1 parent 6db30a0 commit 9820f39
Show file tree
Hide file tree
Showing 14 changed files with 10 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.dangdang.ddframe.job.api.JobType;
import com.dangdang.ddframe.job.cloud.executor.fixture.TestJob;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.event.type.JobTraceEvent;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.google.protobuf.ByteString;
import org.apache.commons.lang3.SerializationUtils;
Expand Down Expand Up @@ -95,8 +94,7 @@ public void assertLaunchTaskWithTransientTaskAndJavaScriptJob() {
@Test
public void assertRegisteredWithoutData() {
ExecutorInfo executorInfo = ExecutorInfo.newBuilder().setExecutorId(Protos.ExecutorID.newBuilder().setValue("test_executor")).setCommand(Protos.CommandInfo.getDefaultInstance())
.setData(ByteString.copyFrom(SerializationUtils.serialize(new JobEventRdbConfiguration("org.h2.Driver", "jdbc:h2:mem:test_executor", "sa", "", JobTraceEvent.LogLevel.INFO))))
.build();
.setData(ByteString.copyFrom(SerializationUtils.serialize(new JobEventRdbConfiguration("org.h2.Driver", "jdbc:h2:mem:test_executor", "sa", "")))).build();
taskExecutor.registered(executorDriver, executorInfo, frameworkInfo, slaveInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.dangdang.ddframe.job.cloud.scheduler.boot.env;

import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.event.type.JobTraceEvent;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -113,7 +112,7 @@ public Optional<JobEventRdbConfiguration> getRdbConfiguration() {
String username = getValue(EnvironmentArgument.EVENT_TRACE_RDB_USERNAME);
String password = getValue(EnvironmentArgument.EVENT_TRACE_RDB_PASSWORD);
if (!Strings.isNullOrEmpty(driver) && !Strings.isNullOrEmpty(url) && !Strings.isNullOrEmpty(username)) {
return Optional.of(new JobEventRdbConfiguration(driver, url, username, password, JobTraceEvent.LogLevel.INFO));
return Optional.of(new JobEventRdbConfiguration(driver, url, username, password));
}
return Optional.absent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.dangdang.ddframe.job.event.type.JobExecutionEvent;
import com.dangdang.ddframe.job.event.type.JobStatusTraceEvent;
import com.dangdang.ddframe.job.event.type.JobTraceEvent;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;

Expand All @@ -31,15 +30,6 @@
*/
public interface JobEventListener extends JobEventIdentity {

/**
* 作业运行痕迹事件监听执行.
*
* @param jobTraceEvent 作业运行痕迹事件
*/
@Subscribe
@AllowConcurrentEvents
void listen(JobTraceEvent jobTraceEvent);

/**
* 作业执行事件监听执行.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.JobEventListener;
import com.dangdang.ddframe.job.event.type.JobTraceEvent.LogLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -47,8 +46,6 @@ public class JobEventRdbConfiguration extends JobEventRdbIdentity implements Job

private final String password;

private final LogLevel logLevel;

@Override
public JobEventListener createJobEventListener() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.dangdang.ddframe.job.event.JobEventListener;
import com.dangdang.ddframe.job.event.type.JobExecutionEvent;
import com.dangdang.ddframe.job.event.type.JobStatusTraceEvent;
import com.dangdang.ddframe.job.event.type.JobTraceEvent;

import java.sql.SQLException;

Expand All @@ -34,12 +33,7 @@ public final class JobEventRdbListener extends JobEventRdbIdentity implements Jo
private final JobEventRdbStorage repository;

public JobEventRdbListener(final JobEventRdbConfiguration config) throws SQLException {
repository = new JobEventRdbStorage(config.getDriverClassName(), config.getUrl(), config.getUsername(), config.getPassword(), config.getLogLevel());
}

@Override
public void listen(final JobTraceEvent traceEvent) {
repository.addJobTraceEvent(traceEvent);
repository = new JobEventRdbStorage(config.getDriverClassName(), config.getUrl(), config.getUsername(), config.getPassword());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import com.dangdang.ddframe.job.event.type.JobExecutionEvent;
import com.dangdang.ddframe.job.event.type.JobStatusTraceEvent;
import com.dangdang.ddframe.job.event.type.JobTraceEvent;
import com.dangdang.ddframe.job.event.type.JobTraceEvent.LogLevel;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -41,30 +39,20 @@
@Slf4j
class JobEventRdbStorage {

private static final String TABLE_JOB_TRACE_LOG = "JOB_TRACE_LOG";

private static final String TABLE_JOB_EXECUTION_LOG = "JOB_EXECUTION_LOG";

private static final String TABLE_JOB_STATUS_TRACE_LOG = "JOB_STATUS_TRACE_LOG";

private final DataSource dataSource;

private final LogLevel logLevel;

JobEventRdbStorage(final String driverClassName, final String url, final String username, final String password, final LogLevel logLevel) throws SQLException {
this.logLevel = logLevel;
JobEventRdbStorage(final String driverClassName, final String url, final String username, final String password) throws SQLException {
dataSource = JobEventRdbDataSourceFactory.getDataSource(driverClassName, url, username, password);
initTables();
}

private void initTables() throws SQLException {
try (Connection conn = dataSource.getConnection()) {
DatabaseMetaData dbMetaData = conn.getMetaData();
try (ResultSet resultSet = dbMetaData.getTables(null, null, TABLE_JOB_TRACE_LOG, new String[]{"TABLE"})) {
if (!resultSet.next()) {
createJobTraceTable(conn);
}
}
try (ResultSet resultSet = dbMetaData.getTables(null, null, TABLE_JOB_EXECUTION_LOG, new String[]{"TABLE"})) {
if (!resultSet.next()) {
createJobExecutionTable(conn);
Expand All @@ -78,22 +66,6 @@ private void initTables() throws SQLException {
}
}

private void createJobTraceTable(final Connection conn) throws SQLException {
String dbSchema = "CREATE TABLE `" + TABLE_JOB_TRACE_LOG + "` ("
+ "`id` VARCHAR(40) NOT NULL, "
+ "`job_name` VARCHAR(100) NOT NULL, "
+ "`hostname` VARCHAR(255) NOT NULL, "
+ "`ip` VARCHAR(50) NOT NULL, "
+ "`log_level` CHAR(5) NOT NULL, "
+ "`message` VARCHAR(2000) NOT NULL, "
+ "`failure_cause` VARCHAR(4000) NULL, "
+ "`creation_time` TIMESTAMP NOT NULL, "
+ "PRIMARY KEY (`id`));";
try (PreparedStatement preparedStatement = conn.prepareStatement(dbSchema)) {
preparedStatement.execute();
}
}

private void createJobExecutionTable(final Connection conn) throws SQLException {
String dbSchema = "CREATE TABLE `" + TABLE_JOB_EXECUTION_LOG + "` ("
+ "`id` VARCHAR(40) NOT NULL, "
Expand Down Expand Up @@ -132,31 +104,6 @@ private void createJobStatusTraceTable(final Connection conn) throws SQLExceptio
}
}

boolean addJobTraceEvent(final JobTraceEvent traceEvent) {
boolean result = false;
if (needTrace(traceEvent.getLogLevel())) {
String sql = "INSERT INTO `" + TABLE_JOB_TRACE_LOG + "` (`id`, `job_name`, `hostname`, `ip`, `log_level`, `message`, `failure_cause`, `creation_time`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
try (
Connection conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.setString(2, traceEvent.getJobName());
preparedStatement.setString(3, traceEvent.getHostname());
preparedStatement.setString(4, traceEvent.getIp());
preparedStatement.setString(5, traceEvent.getLogLevel().name());
preparedStatement.setString(6, traceEvent.getMessage());
preparedStatement.setString(7, truncateString(traceEvent.getFailureCause()));
preparedStatement.setTimestamp(8, new Timestamp(traceEvent.getCreationTime().getTime()));
preparedStatement.execute();
result = true;
} catch (final SQLException ex) {
// TODO 记录失败直接输出日志,未来可考虑配置化
log.error(ex.getMessage());
}
}
return result;
}

boolean addJobExecutionEvent(final JobExecutionEvent jobExecutionEvent) {
boolean result = false;
if (null == jobExecutionEvent.getCompleteTime()) {
Expand Down Expand Up @@ -241,10 +188,6 @@ boolean addJobStatusTraceEvent(final JobStatusTraceEvent jobStatusTraceEvent) {
return result;
}

private boolean needTrace(final LogLevel logLevel) {
return logLevel.ordinal() >= this.logLevel.ordinal();
}

private String truncateString(final String str) {
return !Strings.isNullOrEmpty(str) && str.length() > 4000 ? str.substring(0, 4000) : str;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
JobTraceEventTest.class,
JobExecutionEventTest.class,
JobEventBusTest.class,
JobEventRdbIdentityTest.class,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,29 @@
import com.dangdang.ddframe.job.event.JobEventListener;
import com.dangdang.ddframe.job.event.type.JobExecutionEvent;
import com.dangdang.ddframe.job.event.type.JobStatusTraceEvent;
import com.dangdang.ddframe.job.event.type.JobTraceEvent;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public final class TestJobEventListener extends TestJobEventIdentity implements JobEventListener {

@Getter
private static volatile boolean traceEventCalled;

@Getter
private static volatile boolean executionEventCalled;

@Getter
private static volatile boolean statusTraceEventCalled;

private final JobEventCaller jobEventCaller;

@Override
public void listen(final JobTraceEvent traceEvent) {
jobEventCaller.call();
traceEventCalled = true;
}

@Override
public void listen(final JobExecutionEvent jobExecutionEvent) {
jobEventCaller.call();
executionEventCalled = true;
}

@Override
public void listen(JobStatusTraceEvent jobStatusTraceEvent) {
public void listen(final JobStatusTraceEvent jobStatusTraceEvent) {
jobEventCaller.call();
statusTraceEventCalled = true;
}

public static void reset() {
traceEventCalled = false;
executionEventCalled = false;
statusTraceEventCalled = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.dangdang.ddframe.job.event.rdb;

import com.dangdang.ddframe.job.event.type.JobTraceEvent;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
Expand All @@ -28,12 +27,11 @@ public final class JobEventRdbConfigurationTest {

@Test
public void assertCreateJobEventListenerSuccess() {
assertThat(
new JobEventRdbConfiguration("org.h2.Driver", "jdbc:h2:mem:job_event_storage", "sa", "", JobTraceEvent.LogLevel.INFO).createJobEventListener(), instanceOf(JobEventRdbListener.class));
assertThat(new JobEventRdbConfiguration("org.h2.Driver", "jdbc:h2:mem:job_event_storage", "sa", "").createJobEventListener(), instanceOf(JobEventRdbListener.class));
}

@Test
public void assertCreateJobEventListenerFailure() {
assertNull(new JobEventRdbConfiguration("", "", "", "", JobTraceEvent.LogLevel.INFO).createJobEventListener());
assertNull(new JobEventRdbConfiguration("", "", "", "").createJobEventListener());
}
}
Loading

0 comments on commit 9820f39

Please sign in to comment.