Ejemplo n.º 1
0
 private static String responseMessage(String action, MySQLFrontConnection c, long id) {
   return new StringBuilder("RESPONSE:")
       .append(action)
       .append(", id=")
       .append(id)
       .append(", host=")
       .append(c.getHost())
       .append(", port=")
       .append(c.getPort())
       .append(", time=")
       .append(TimeUtil.currentTimeMillis())
       .toString();
 }
Ejemplo n.º 2
0
 public static void execute(MySQLFrontConnection c) {
   final ReentrantLock lock = MycatServer.getInstance().getConfig().getLock();
   lock.lock();
   try {
     if (ReloadUtil.rollback()) {
       StringBuilder s = new StringBuilder();
       s.append(c).append("Rollback config success by manager");
       LOGGER.warn(s.toString());
       OkPacket ok = new OkPacket();
       ok.packetId = 1;
       ok.affectedRows = 1;
       ok.serverStatus = 2;
       ok.message = "Rollback config success".getBytes();
       ok.write(c);
     } else {
       c.writeErrMessage(ErrorCode.ER_YES, "Rollback config failure");
     }
   } finally {
     lock.unlock();
   }
 }
Ejemplo n.º 3
0
 public static void processSQL(
     MySQLFrontConnection sc, SchemaConfig schema, String sql, int sqlType) {
   MycatServer.getInstance()
       .getSequnceProcessor()
       .addNewSql(new SessionSQLPair(sc.getSession2(), schema, sql, sqlType));
 }
Ejemplo n.º 4
0
  public static boolean processERChildTable(
      final SchemaConfig schema, final String origSQL, final MySQLFrontConnection sc)
      throws SQLNonTransientException {
    String tableName = StringUtil.getTableName(origSQL).toUpperCase();
    final TableConfig tc = schema.getTables().get(tableName);

    if (null != tc && tc.isChildTable()) {
      final RouteResultset rrs = new RouteResultset(origSQL, ServerParse.INSERT);
      String joinKey = tc.getJoinKey();
      MySqlInsertStatement insertStmt =
          (MySqlInsertStatement) (new MySqlStatementParser(origSQL)).parseInsert();
      int joinKeyIndex = getJoinKeyIndex(insertStmt.getColumns(), joinKey);

      if (joinKeyIndex == -1) {
        String inf = "joinKey not provided :" + tc.getJoinKey() + "," + insertStmt;
        LOGGER.warn(inf);
        throw new SQLNonTransientException(inf);
      }
      if (isMultiInsert(insertStmt)) {
        String msg = "ChildTable multi insert not provided";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
      }

      String joinKeyVal = insertStmt.getValues().getValues().get(joinKeyIndex).toString();

      String sql = insertStmt.toString();

      // try to route by ER parent partion key
      // RouteResultset theRrs = RouterUtil.routeByERParentKey(sc,schema,ServerParse.INSERT,sql,
      // rrs, tc, joinKeyVal);

      if (null != null) {
        boolean processedInsert = false;
        if (sc != null && tc.isAutoIncrement()) {
          String primaryKey = tc.getPrimaryKey();
          processedInsert =
              processInsert(sc, schema, ServerParse.INSERT, sql, tc.getName(), primaryKey);
        }
        if (processedInsert == false) {
          rrs.setFinishedRoute(true);
          sc.getSession2().execute(rrs, ServerParse.INSERT);
        }
        return true;
      }

      // route by sql query root parent's datanode
      final String findRootTBSql = tc.getLocateRTableKeySql().toLowerCase() + joinKeyVal;
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("find root parent's node sql " + findRootTBSql);
      }

      ListenableFuture<String> listenableFuture =
          MycatServer.getInstance()
              .getListeningExecutorService()
              .submit(
                  new Callable<String>() {
                    @Override
                    public String call() throws Exception {
                      FetchStoreNodeOfChildTableHandler fetchHandler =
                          new FetchStoreNodeOfChildTableHandler();
                      return fetchHandler.execute(
                          schema.getName(), findRootTBSql, tc.getRootParent().getDataNodes());
                    }
                  });

      Futures.addCallback(
          listenableFuture,
          new FutureCallback<String>() {
            @Override
            public void onSuccess(String result) {
              if (Strings.isNullOrEmpty(result)) {
                StringBuilder s = new StringBuilder();
                LOGGER.warn(
                    s.append(sc.getSession2()).append(origSQL).toString()
                        + " err:"
                        + "can't find (root) parent sharding node for sql:"
                        + origSQL);
                sc.writeErrMessage(
                    ErrorCode.ER_PARSE_ERROR,
                    "can't find (root) parent sharding node for sql:" + origSQL);
                return;
              }

              if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(
                    "found partion node for child table to insert " + result + " sql :" + origSQL);
              }
              boolean processedInsert = false;
              if (sc != null && tc.isAutoIncrement()) {
                try {
                  String primaryKey = tc.getPrimaryKey();
                  processedInsert =
                      processInsert(
                          sc, schema, ServerParse.INSERT, origSQL, tc.getName(), primaryKey);
                } catch (SQLNonTransientException e) {
                  LOGGER.warn("sequence processInsert error,", e);
                  sc.writeErrMessage(
                      ErrorCode.ER_PARSE_ERROR, "sequence processInsert error," + e.getMessage());
                }
              }
              if (processedInsert == false) {
                RouteResultset executeRrs = RouterUtil.routeToSingleNode(rrs, result, origSQL);
                sc.getSession2().execute(executeRrs, ServerParse.INSERT);
              }
            }

            @Override
            public void onFailure(Throwable t) {
              StringBuilder s = new StringBuilder();
              LOGGER.warn(
                  s.append(sc.getSession2()).append(origSQL).toString() + " err:" + t.getMessage());
              sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, t.getMessage() + " " + s.toString());
            }
          },
          MycatServer.getInstance().getListeningExecutorService());
      return true;
    }
    return false;
  }