lealone / Lealone

比 MySQL 和 MongoDB 快10倍的 OLTP 关系数据库和文档数据库
Other
2.48k stars 520 forks source link

重新启动运行创建数据库脚本报错 #243

Open cbqqkcel opened 4 weeks ago

cbqqkcel commented 4 weeks ago
16:58:40.867 [Thread-0] INFO  com.lealone.main.Lealone - Lealone version: 6.1.0-SNAPSHOT
16:58:40.878 [Thread-0] INFO  com.lealone.main.config.YamlConfigLoader - Loading config from file:/D:/java/dmy/dmy-ts/dmy-ts-server/dmy-ts-server-admin-lealone-starter/target/classes/lealone.yaml
16:58:40.944 [Thread-0] INFO  com.lealone.main.Lealone - Base dir: D:/java/dmy/dmy-ts/db
16:58:40.950 [Thread-0] INFO  com.lealone.main.Lealone - Init storage engines: 4 ms
16:58:40.980 [Thread-0] INFO  com.lealone.main.Lealone - Init transaction engines: 30 ms
16:58:40.985 [Thread-0] INFO  com.lealone.main.Lealone - Init sql engines: 4 ms
16:58:40.990 [Thread-0] INFO  com.lealone.main.Lealone - Init protocol server engines: 5 ms
16:58:41.110 [ScheduleService-0] INFO  com.lealone.main.Lealone - Init lealone database: 117 ms
16:58:41.110 [ScheduleService-0] INFO  com.lealone.main.Lealone - Start TcpServer, host: 0.0.0.0, port: 9210
16:58:41.113 [ScheduleService-0] INFO  com.lealone.main.Lealone - Total time: 240 ms (Load config: 56 ms, Init: 181 ms, Start: 3 ms)
16:58:41.116 [ScheduleService-0] INFO  com.lealone.main.Lealone - Lealone started, jvm pid: 19296, exit with ctrl+c or execute sql command: stop server
249  2024-10-30 16:58:41.377 [main] DEBUG cn.com.idmy.cloud.util.LealoneSqlUtil [63] - execute sql: create database if not exists DMY_TS parameters(database_to_upper=false)
2024-10-30 16:58:41 database: CREATE CACHED TABLE PUBLIC."Task"(
    "id" INT DEFAULT (NEXT VALUE FOR PUBLIC."Task") NOT NULL,
    "pid" INT DEFAULT 0 NOT NULL,
    "appId" INT DEFAULT 0 NOT NULL,
    "name" VARCHAR NOT NULL,
    "creatorId" LONG,
    "creator" VARCHAR,
    "createdAt" TIMESTAMP NOT NULL,
    "updatedAt" TIMESTAMP NOT NULL,
    "maxLogQty" INT DEFAULT 10 NOT NULL,
    "target" VARCHAR NOT NULL,
    "concurrent" BOOLEAN DEFAULT FALSE NOT NULL,
    "timeout" INT DEFAULT 6 NOT NULL,
    "retry" INT DEFAULT 0 NOT NULL,
    "triggerLastAt" LONG DEFAULT 0 NOT NULL,
    "triggerNextAt" LONG DEFAULT 0 NOT NULL,
    "scheduleType" ENUM('CRON', 'FIXED_RATE', 'ONCE') DEFAULT 'CRON' NOT NULL,
    "misfireStrategy" ENUM('IGNORE', 'IMMEDIATE') DEFAULT 'IMMEDIATE' NOT NULL,
    "routeStrategy" ENUM('FIRST', 'LAST', 'ROUND', 'RANDOM', 'HASH', 'LFU', 'LRU', 'IDLE', 'SHARDING') DEFAULT 'FIRST' NOT NULL,
    "triggerStatus" ENUM('STOP', 'RUN') DEFAULT 'STOP' NOT NULL,
    "scheduleConfig" VARCHAR NOT NULL,
    "params" VARCHAR
) PARAMETERS(RUN_MODE='CLIENT_SERVER')
2024-10-30 16:58:41 database: opening DMY_TS
16:58:41.510 [ScheduleService-8] WARN  com.lealone.server.scheduler.GlobalScheduler - Failed to run misc task: com.lealone.server.scheduler.GlobalScheduler$1@49c0a661
com.lealone.common.exceptions.JdbcSQLException: General error: "java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 2"; SQL statement:
CREATE CACHED TABLE PUBLIC."Task"(
    "id" INT DEFAULT (NEXT VALUE FOR PUBLIC."Task") NOT NULL,
    "pid" INT DEFAULT 0 NOT NULL,
    "appId" INT DEFAULT 0 NOT NULL,
    "name" VARCHAR NOT NULL,
    "creatorId" LONG,
    "creator" VARCHAR,
@Slf4j
public class LealoneSqlUtil {
    static String lastJdbcUrl;

    @SneakyThrows
    public static String run(String db) {
        runSql("jdbc:lealone:embed:lealone", format("create database if not exists {} parameters(database_to_upper=false)", db));
        lastJdbcUrl = format("jdbc:lealone:embed:{}?trace_level_system_out=1&network_timeout=2147483647", db);
        runTable(lastJdbcUrl);
        runInit(lastJdbcUrl);
        return lastJdbcUrl;
    }

    @SneakyThrows
    static void runTable(String jdbcUrl) {
        List<String> filenames = IoUtil.readUtf8Lines(LealoneSqlUtil.class.getClassLoader().getResourceAsStream("sql/tables"), new ArrayList<>());
        for (String filename : filenames) {
            String sql = IoUtil.readUtf8(LealoneSqlUtil.class.getClassLoader().getResourceAsStream("sql/table/" + filename));
            runSql(jdbcUrl, sql);
        }
    }

    static void runInit(String jdbcUrl) {
        List<String> filenames = IoUtil.readUtf8Lines(LealoneSqlUtil.class.getClassLoader().getResourceAsStream("sql/inits"), new ArrayList<>());
        for (String filename : filenames) {
            String init = IoUtil.readUtf8(LealoneSqlUtil.class.getClassLoader().getResourceAsStream("sql/init/" + filename));
            List<String> sqls = SplitUtil.split(init, ";");
            for (String sql : sqls) {
                try {
                    runSql(jdbcUrl, sql);
                } catch (Exception ignored) {
                }
            }
        }
    }

    static void runSql(String jdbcUrl, String sql) throws Exception {
        Connection conn = null;
        Statement st = null;
        try {
            conn = DriverManager.getConnection(jdbcUrl);
            st = conn.createStatement();
            st.executeUpdate(sql);
            log.debug("execute sql: " + sql);
        } finally {
            IoUtil.closeQuietly(st, conn);
        }
    }

    static void runScript(String jdbcUrl, String path) throws Exception {
        runSql(jdbcUrl, "runscript from '" + path + "'");
    }

    public static Pair<String, String> runBackup() throws Exception {
        String filename = DateUtil.format(new Date(), "yyyy-MM-dd-HH") + ".zip";
        String path = format("{}/{}/{}", System.getProperty("user.dir"), "backup", filename);
        runSql(lastJdbcUrl, format("backup to '{}'", path));
        return Pair.of(filename, path);
    }
}
public class LealoneTsAdminApplication {
    public static void main(String[] args) {
        Lealone.main(args, () -> {
            String[] args2 = ArrayUtil.append(args, "--spring.datasource.url=" + LealoneSqlUtil.run("DMY_TS"));
            SpringApplication.run(LealoneTsAdminApplication.class, args2);
        });
    }
}
create sequence if not exists App start with 1;
create table if not exists App
(
    id        long default (next value for App) primary key,
    "key"     varchar not null,
    name      varchar not null,
    createdAt timestamp,
    updatedAt timestamp
);

create sequence if not exists Task start with 1;
create table if not exists Task
(
    id              int                                                                                 not null default (next value for Task) primary key,
    pid             int                                                                                 not null default 0,
    appId           int                                                                                 not null default 0,
    name            varchar                                                                             not null,
    creatorId       long,
    creator         varchar,
    createdAt       timestamp                                                                           not null,
    updatedAt       timestamp                                                                           not null,
    maxLogQty       int                                                                                 not null default 10,
    target          varchar                                                                             not null,
    concurrent      boolean                                                                             not null default false,
    timeout         int                                                                                 not null default 6,
    retry           int                                                                                 not null default 0,
    triggerLastAt   long                                                                                not null default 0,
    triggerNextAt   long                                                                                not null default 0,
    scheduleType    enum ('CRON', 'FIXED_RATE', 'ONCE')                                                 not null default 'CRON',
    misfireStrategy enum ('IGNORE', 'IMMEDIATE')                                                        not null default 'IMMEDIATE',
    routeStrategy   enum ('FIRST', 'LAST', 'ROUND', 'RANDOM', 'HASH', 'LFU', 'LRU', 'IDLE', 'SHARDING') not null default 'FIRST',
    triggerStatus   enum ('STOP', 'RUN')                                                                not null default 'STOP',
    scheduleConfig  varchar                                                                             not null,
    params          varchar
);

create sequence if not exists TaskLog start with 1;
create table if not exists TaskLog
(
    id          int                                                                                                     not null default (next value for TaskLog) primary key,
    taskId      int                                                                                                     not null,
    address     varchar                                                                                                 not null,
    createdAt   timestamp                                                                                               not null,
    updatedAt   timestamp                                                                                               not null,
    callbackAt  timestamp,
    timeout     timestamp                                                                                               not null,
    content     varchar,
    retry       int                                                                                                     not null,
    status      enum ('UNREQUESTED', 'REQUEST_ERROR', 'WAIT_RESPONSE', 'RESPONSE_ERROR', 'RESPONSE_TIMEOUT', 'SUCCESS') not null default 'UNREQUESTED',
    triggerType enum ('MANUAL', 'CRON', 'RETRY', 'PARENT', 'API', 'MISFIRE')                                            not null default 'CRON'
);

create table if not exists Executor
(
    address     varchar   not null primary key,
    appId       long      not null default 0,
    appKey      varchar   not null,
    createdAt   timestamp not null,
    updatedAt   timestamp not null,
    heartbeatAt timestamp not null
);

create table if not exists Lock
(
    name    varchar not null primary key,
    node    varchar,
    lockAt  long,
    timeout long    not null
);
cbqqkcel commented 4 weeks ago

删掉db文件第一次启动什么问题都没有 重启后会在 runTable(lastJdbcUrl); 后报错。

runSql("jdbc:lealone:embed:lealone", 这行还不会报错。 而且我的create语句都加了 if not exists

cbqqkcel commented 4 weeks ago

runTable(lastJdbcUrl); 执行到这里就报错了 DriverManager.getConnection(jdbcUrl)

codefollower commented 4 weeks ago

你用的是最新的代码还是 6.0.1? 最新的代码还很新,我还没有跑测试。

cbqqkcel commented 4 weeks ago

最新的6.1.0-SNAPSHOT