Search in sources :

Example 11 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class ForceSwitchableDataSource method forceSwitch.

public SwitchableDataSourceStatus forceSwitch(FirstAidKit configure, final String ip, final Integer port) {
    synchronized (lock) {
        final SwitchableDataSourceStatus oldStatus = getStatus();
        final String name;
        final DataSourceConfigure usedConfigure;
        forceSwitchedStatus = ForceSwitchedStatus.ForceSwitching;
        try {
            if (isNullDataSource) {
                if (configure instanceof IDataSourceConfigure) {
                    usedConfigure = dataSourceConfigureConvert.desDecrypt(DataSourceConfigure.valueOf((IDataSourceConfigure) configure));
                    name = ((IDataSourceConfigure) configure).getConnectionUrl();
                } else {
                    throw new DalRuntimeException("Force Switch Error: datasource configure is invalid");
                }
            } else {
                usedConfigure = getSingleDataSource().getDataSourceConfigure().clone();
                name = getSingleDataSource().getName();
            }
            final String logName = String.format(FORCE_SWITCH, name);
            LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, String.format("isNullDataSource: %s; newIp: %s, newPort: %s", isNullDataSource, ip, port), new Callback() {

                @Override
                public void execute() throws Exception {
                    LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old connection url: %s", usedConfigure.getConnectionUrl()));
                    LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before force switch: %s, old poolCreated before force switch: %s", oldStatus.isForceSwitched(), oldStatus.isPoolCreated()));
                    usedConfigure.replaceURL(ip, port);
                    LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new connection url: %s", usedConfigure.getConnectionUrl()));
                    asyncRefreshDataSource(name, usedConfigure, new ForceSwitchListener() {

                        public void onCreatePoolSuccess() {
                            LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolSuccess: %s", name), usedConfigure.getConnectionUrl());
                            poolCreated = true;
                            forceSwitchedStatus = ForceSwitchedStatus.ForceSwitched;
                            oldForceSwitchedStatus = ForceSwitchedStatus.ForceSwitched;
                            isNullDataSource = false;
                            final SwitchableDataSourceStatus status = getStatus();
                            LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolSuccess::notifyListeners: %s", name), "notify listeners' onForceSwitchSuccess");
                            for (final SwitchListener listener : listeners) executor.submit(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        listener.onForceSwitchSuccess(status);
                                    } catch (Exception e) {
                                        LOGGER.error("call listener.onForceSwitchSuccess() error ", e);
                                    }
                                }
                            });
                        }

                        public void onCreatePoolFail(final Throwable e) {
                            LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolFail: %s", name), e.getMessage());
                            forceSwitchedStatus = oldForceSwitchedStatus;
                            final SwitchableDataSourceStatus status = getStatus();
                            LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolFail::notifyListeners: %s", name), "notify listeners' onForceSwitchFail");
                            for (final SwitchListener listener : listeners) executor.submit(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        listener.onForceSwitchFail(status, e);
                                    } catch (Exception e1) {
                                        LOGGER.error("call listener.onForceSwitchFail() error ", e1);
                                    }
                                }
                            });
                        }
                    });
                }
            });
        } catch (Throwable t) {
            forceSwitchedStatus = oldForceSwitchedStatus;
            LOGGER.error("Force switch error", t);
            throw new DalRuntimeException("Force switch error", t);
        }
        return oldStatus;
    }
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Callback(com.ctrip.platform.dal.dao.log.Callback) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) SQLException(java.sql.SQLException)

Example 12 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class ClusterDynamicDataSource method prepare.

protected void prepare() {
    cluster.addListener(event -> {
        if (status.get() == ForceSwitchedStatus.UnForceSwitched) {
            try {
                switchDataSource();
            } catch (Throwable t) {
                String msg = "Cluster switch listener error";
                LOGGER.error(msg, t);
                throw new DalRuntimeException(msg, t);
            }
        } else
            LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, String.format("switchIgnored:%s", cluster.getClusterName()), "");
    });
    switchDataSource();
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString)

Example 13 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class ClusterDynamicDataSource method restore.

@Override
public SwitchableDataSourceStatus restore() {
    synchronized (lock) {
        SwitchableDataSourceStatus currentStatus = getStatus();
        try {
            String logName = String.format(RESTORE, clusterInfo.toString());
            LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, "restore", () -> {
                LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before restore: %s, old poolCreated before restore: %s", currentStatus.isForceSwitched(), currentStatus.isPoolCreated()));
                if (status.get() != ForceSwitchedStatus.ForceSwitched) {
                    LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, "not in force switched status");
                    return;
                }
                LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("previous host(s): %s:%s", currentStatus.getHostName(), currentStatus.getPort()));
                HostAndPort newHost = buildHostAndPort(cluster);
                LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new host(s): %s:%s", newHost.getHost(), newHost.getPort()));
                getExecutor().submit(() -> {
                    try {
                        switchDataSource();
                        status.set(ForceSwitchedStatus.UnForceSwitched);
                    } catch (Throwable t) {
                        LOGGER.error("DataSource restoring failed", t);
                    }
                });
            });
            return currentStatus;
        } catch (Throwable t) {
            LOGGER.error("Restore error", t);
            throw new DalRuntimeException("Restore error", t);
        }
    }
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString)

Example 14 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class SingleDeleteTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, fields, parser.getPrimaryKeyNames());
    String tableName = getRawTableName(hints, parameters, fields);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    String deleteSql = buildDeleteSql(quote(tableName));
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(deleteSql, parameters, hints.setFields(fields), taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 15 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class SingleUpdateTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
    if (fields.size() == 0)
        throw new DalException(ErrorCode.ValidateFieldCount);
    Map<String, ?> pks = getPrimaryKeys(fields);
    Object version = getVersion(fields);
    Map<String, ?> filted = null;
    if (rawPojo instanceof UpdatableEntity)
        filted = filterUpdatableEntity(hints, fields, getUpdatedColumns(rawPojo));
    else
        filted = filterNullColumns(hints, fields);
    // If there is no columns changed, we will not perform update
    if (filted.size() == 0)
        return 0;
    String tableName = getRawTableName(hints, fields);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    String updateSql = buildUpdateSql(quote(tableName), filted, hints);
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, filted);
    addParameters(parameters, pks);
    addVersion(parameters, version);
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(updateSql, parameters, hints.setFields(fields), taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) DalException(com.ctrip.platform.dal.exceptions.DalException) UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Aggregations

DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)29 DalContextClient (com.ctrip.platform.dal.dao.DalContextClient)8 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)8 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 Callback (com.ctrip.platform.dal.dao.log.Callback)3 Map (java.util.Map)3 Cluster (com.ctrip.framework.dal.cluster.client.Cluster)2 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)2 RouteStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)2 Node (org.w3c.dom.Node)2 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)1 Listener (com.ctrip.framework.dal.cluster.client.base.Listener)1 ClusterConfig (com.ctrip.framework.dal.cluster.client.config.ClusterConfig)1 DalConfigCustomizedOption (com.ctrip.framework.dal.cluster.client.config.DalConfigCustomizedOption)1 ClusterConfigException (com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException)1 CustomDataSourceFactory (com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)1 DatabaseShard (com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)1 ClusterIdGeneratorConfig (com.ctrip.framework.dal.cluster.client.sharding.idgen.ClusterIdGeneratorConfig)1