Skip to content

Commit

Permalink
feat: Added periodic.schedule.update-stock.cap.size
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Aug 22, 2024
1 parent 7c132a7 commit a56ffea
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ public interface StockHistoryDao {
*/
List<StockHistoryDateTime> selectDistinctDateTime();

/**
* <pre>
* delete from stock_history
* where s_date = :date and s_time = :time
* </pre>
*/
int deleteByDateTime(LocalDate date, LocalTime time);

/**
* <pre>
* delete from stock_history
* where s_date = :date and s_time = :time and s_f_id = :factoryId
* </pre>
*/
int deleteByDateTime(LocalDate date, LocalTime time, int factoryId);

/**
* <pre>
* delete from stock_history
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public CostBenchDbManagerIceaxe(DbManagerPurpose purpose, boolean isMultiSession
var credential = new UsernamePasswordCredential(BenchConst.tsurugiUser(), BenchConst.tsurugiPassword());
this.connector = TsurugiConnector.of(endpoint, credential);
try {
this.sessionOption = TgSessionOption.of();
this.sessionOption = TgSessionOption.of().setApplicationName("CostBenchDbManagerIceaxe");
if (!isMultiSession) {
var session = connector.createSession(sessionOption);
var tm = session.createTransactionManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistoryDateTime;
import com.tsurugidb.benchmark.costaccounting.db.iceaxe.CostBenchDbManagerIceaxe;
import com.tsurugidb.benchmark.costaccounting.db.iceaxe.domain.BenchVariable;
import com.tsurugidb.benchmark.costaccounting.util.BenchConst;
import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters;
import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable;
import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableInteger;
Expand Down Expand Up @@ -99,6 +100,38 @@ protected void initialize() {
}
};

@Override
public int deleteByDateTime(LocalDate date, LocalTime time) {
var ps = deleteByDateTimeCache.get();
var parameter = TgBindParameters.of(vDate.bind(date), vTime.bind(time));
return executeAndGetCount(ps, parameter);
}

private final CachePreparedStatement<TgBindParameters> deleteByDateTimeCache = new CachePreparedStatement<>() {
@Override
protected void initialize() {
this.sql = "delete from " + TABLE_NAME //
+ " where s_date = " + vDate + " and s_time = " + vTime;
this.parameterMapping = TgParameterMapping.of(vDate, vTime);
}
};

@Override
public int deleteByDateTime(LocalDate date, LocalTime time, int factoryId) {
var ps = deleteByDateTimeFactoryCache.get();
var parameter = TgBindParameters.of(vDate.bind(date), vTime.bind(time), vFactory.bind(factoryId));
return executeAndGetCount(ps, parameter);
}

private final CachePreparedStatement<TgBindParameters> deleteByDateTimeFactoryCache = new CachePreparedStatement<>() {
@Override
protected void initialize() {
this.sql = "delete from " + TABLE_NAME //
+ " where s_date = " + vDate + " and s_time = " + vTime + " and s_f_id = " + vFactory;
this.parameterMapping = TgParameterMapping.of(vDate, vTime, vFactory);
}
};

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time) {
var ps = deleteOldDateTimeCache.get();
Expand All @@ -109,8 +142,11 @@ public int deleteOldDateTime(LocalDate date, LocalTime time) {
private final CachePreparedStatement<TgBindParameters> deleteOldDateTimeCache = new CachePreparedStatement<>() {
@Override
protected void initialize() {
this.sql = "delete from " + TABLE_NAME //
+ " where (s_date < " + vDate + ") or (s_date = " + vDate + " and s_time <= " + vTime + ")";
String where = "(s_date < " + vDate + ") or (s_date = " + vDate + " and s_time <= " + vTime + ")";
if (BenchConst.WORKAROUND) { // orでつなぐとfull scanになる為
where = "(" + where + ") and s_date <= " + vDate;
}
this.sql = "delete from " + TABLE_NAME + " where " + where;
this.parameterMapping = TgParameterMapping.of(vDate, vTime);
}
};
Expand All @@ -125,8 +161,11 @@ public int deleteOldDateTime(LocalDate date, LocalTime time, int factoryId) {
private final CachePreparedStatement<TgBindParameters> deleteOldDateTimeFactoryCache = new CachePreparedStatement<>() {
@Override
protected void initialize() {
this.sql = "delete from " + TABLE_NAME //
+ " where ((s_date < " + vDate + ") or (s_date = " + vDate + " and s_time <= " + vTime + ")) and s_f_id = " + vFactory;
String where = "((s_date < " + vDate + ") or (s_date = " + vDate + " and s_time <= " + vTime + ")) and s_f_id = " + vFactory;
if (BenchConst.WORKAROUND) { // orでつなぐとfull scanになる為
where = "(" + where + ") and s_date <= " + vDate;
}
this.sql = "delete from " + TABLE_NAME + " where " + where;
this.parameterMapping = TgParameterMapping.of(vDate, vTime, vFactory);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ public List<StockHistoryDateTime> selectDistinctDateTime() {
});
}

@Override
public int deleteByDateTime(LocalDate date, LocalTime time) {
String sql = "delete from " + TABLE_NAME //
+ " where s_date = ? and s_time = ?";
return executeUpdate(sql, ps -> {
int i = 1;
JdbcUtil.setDate(ps, i++, date);
JdbcUtil.setTime(ps, i++, time);
});
}

@Override
public int deleteByDateTime(LocalDate date, LocalTime time, int factoryId) {
String sql = "delete from " + TABLE_NAME //
+ " where s_date = ? and s_time = ? and s_f_id = ?";
return executeUpdate(sql, ps -> {
int i = 1;
JdbcUtil.setDate(ps, i++, date);
JdbcUtil.setTime(ps, i++, time);
JdbcUtil.setInt(ps, i++, factoryId);
});
}

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time) {
String sql = "delete from " + TABLE_NAME //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public CostBenchDbManagerTsubakuro(DbManagerPurpose purpose) {
LOG.info("endpoint={}", endpoint);
try {
var credential = new UsernamePasswordCredential(BenchConst.tsurugiUser(), BenchConst.tsurugiPassword());
this.session = SessionBuilder.connect(endpoint).withCredential(credential).create();
this.session = SessionBuilder.connect(endpoint).withCredential(credential).withApplicationName("CostBenchDbManagerTsubakuro").create();
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ServerException | InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public List<StockHistoryDateTime> selectDistinctDateTime() {
throw new UnsupportedOperationException("not yet impl");
}

@Override
public int deleteByDateTime(LocalDate date, LocalTime time) {
throw new UnsupportedOperationException("not yet impl");
}

@Override
public int deleteByDateTime(LocalDate date, LocalTime time, int factoryId) {
throw new UnsupportedOperationException("not yet impl");
}

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time) {
throw new UnsupportedOperationException("not yet impl");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.io.Closeable;
import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.tsurugidb.benchmark.costaccounting.online.task.BenchTask;
import com.tsurugidb.benchmark.costaccounting.util.BenchConst;

public abstract class BenchPeriodicTask extends BenchTask implements Closeable {

Expand All @@ -15,8 +17,17 @@ public abstract class BenchPeriodicTask extends BenchTask implements Closeable {
protected List<Integer> factoryList;
// protected LocalDate date;

public BenchPeriodicTask(String tableName, int taskId) {
super(tableName, taskId);
private final int capSize;

private int executeCount = 0;

public BenchPeriodicTask(String taskName, int taskId) {
super(taskName, taskId);

this.capSize = BenchConst.periodicCapSize(taskName);
if (capSize >= 0) {
LOG.info("cap.size={}", capSize);
}
}

public void initialize(List<Integer> factoryList, LocalDate date) {
Expand All @@ -25,7 +36,17 @@ public void initialize(List<Integer> factoryList, LocalDate date) {
}

public final void execute() {
if (!canExecute()) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return;
}

incrementStartCounter();
executeCount++;

boolean exists;
try {
Expand All @@ -42,6 +63,23 @@ public final void execute() {
}
}

private boolean capLog = false;

protected boolean canExecute() {
if (capSize < 0) {
return true;
}

boolean isExecute = executeCount < capSize;
if (!isExecute) {
if (!capLog) {
capLog = true;
LOG.info("executeCount has reached the limit. executeCount={}, capSize={}", executeCount, capSize);
}
}
return isExecute;
}

protected abstract boolean execute1();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class BenchPeriodicUpdateStockTask extends BenchPeriodicTask {
private final int threadSize;
private final ExecutorService service;
private final int keepSize;
private final boolean delete1;

public BenchPeriodicUpdateStockTask(int taskId) {
super(TASK_NAME, taskId);
Expand All @@ -59,13 +60,18 @@ public BenchPeriodicUpdateStockTask(int taskId) {
LOG.info("insert_select_type={}", INSERT_SELECT_TYPE);
this.keepSize = BenchConst.periodicKeepSize(TASK_NAME);
LOG.info("keep.size={}", keepSize);
this.delete1 = BenchConst.periodicDelete1(TASK_NAME);
if (delete1) {
LOG.info("delete1={}", delete1);
}
}

BenchPeriodicUpdateStockTask(int threadSize, int keepSize) { // for test
super(TASK_NAME, 0);
this.threadSize = threadSize;
this.service = null;
this.keepSize = keepSize;
this.delete1 = false;
}

@Override
Expand Down Expand Up @@ -129,22 +135,36 @@ StockHistoryDateTime getDeleteDateTime(List<StockHistoryDateTime> list) {
if (i < 0) {
return null;
}

if (this.delete1) {
return list.get(0); // 最も古い履歴
}

return list.get(i);
}

private boolean executeAll(StockHistoryDateTime deleteDateTime) {
return dbManager.execute(settingMain, () -> {
if (deleteDateTime != null) {
var dao = dbManager.getStockHistoryDao();
dao.deleteOldDateTime(deleteDateTime.getSDate(), deleteDateTime.getSTime());
}

deleteAllInTransaction(deleteDateTime);
executeAllInTransaction();

return true;
});
}

private void deleteAllInTransaction(StockHistoryDateTime deleteDateTime) {
if (deleteDateTime == null) {
return;
}

var dao = dbManager.getStockHistoryDao();
if (this.delete1) {
dao.deleteByDateTime(deleteDateTime.getSDate(), deleteDateTime.getSTime());
} else {
dao.deleteOldDateTime(deleteDateTime.getSDate(), deleteDateTime.getSTime());
}
}

private void executeAllInTransaction() {
var now = LocalDateTime.now();

Expand Down Expand Up @@ -255,11 +275,7 @@ public FactoryTask(int factoryId, LocalDateTime now, StockHistoryDateTime delete
public Void call() throws Exception {
try {
dbManager.execute(settingMain, () -> {
if (deleteDateTime != null) {
var dao = dbManager.getStockHistoryDao();
dao.deleteOldDateTime(deleteDateTime.getSDate(), deleteDateTime.getSTime(), factoryId);
}

deleteInTransaction();
executeInTransaction();
});
} catch (Throwable e) {
Expand All @@ -269,6 +285,19 @@ public Void call() throws Exception {
return null;
}

private void deleteInTransaction() {
if (deleteDateTime == null) {
return;
}

var dao = dbManager.getStockHistoryDao();
if (delete1) {
dao.deleteByDateTime(deleteDateTime.getSDate(), deleteDateTime.getSTime(), factoryId);
} else {
dao.deleteOldDateTime(deleteDateTime.getSDate(), deleteDateTime.getSTime(), factoryId);
}
}

private void executeInTransaction() {
if (INSERT_SELECT_TYPE == InsertSelectType.SELECT_AND_INSERT) {
var costMasterDao = dbManager.getCostMasterDao();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ public static long periodicInterval(String taskName) {
return TimeUnit.SECONDS.toMillis(seconds);
}

public static int periodicCapSize(String taskName) {
return getPropertyInt("periodic.schedule." + taskName + ".cap.size", -1);
}

public static int periodicSplitSize(String taskName) {
return getPropertyInt("periodic.schedule." + taskName + ".split.size", 1);
}
Expand All @@ -415,6 +419,10 @@ public static int periodicKeepSize(String taskName) {
return getPropertyInt("periodic.schedule." + taskName + ".keep.size", -1);
}

public static boolean periodicDelete1(String taskName) {
return getPropertyBoolean("periodic.schedule." + taskName + ".delete1", false);
}

// online-command

public static String onlineCommandLabel() {
Expand Down

0 comments on commit a56ffea

Please sign in to comment.