Exemplo n.º 1
0
  /** Fake an adhoc compiler result and return it to the CI, see if CI initiates the txn. */
  @Test
  public void testFinishedMPAdHocPlanning() throws Exception {
    // Need a batch and a statement
    AdHocPlannedStmtBatch plannedStmtBatch =
        new AdHocPlannedStmtBatch(
            "select * from a",
            null,
            0,
            0,
            "localhost",
            false,
            ProcedureInvocationType.ORIGINAL,
            0,
            0,
            null);
    AdHocPlannedStatement s =
        new AdHocPlannedStatement(
            "select * from a".getBytes(Constants.UTF8ENCODING),
            new CorePlan(
                new byte[0],
                new byte[0],
                new byte[20],
                new byte[20],
                false,
                false,
                true,
                new VoltType[0],
                0),
            ParameterSet.emptyParameterSet(),
            null,
            null,
            null);
    plannedStmtBatch.addStatement(s);
    m_ci.processFinishedCompilerWork(plannedStmtBatch).run();

    ArgumentCaptor<Long> destinationCaptor = ArgumentCaptor.forClass(Long.class);
    ArgumentCaptor<Iv2InitiateTaskMessage> messageCaptor =
        ArgumentCaptor.forClass(Iv2InitiateTaskMessage.class);
    verify(m_messenger).send(destinationCaptor.capture(), messageCaptor.capture());
    Iv2InitiateTaskMessage message = messageCaptor.getValue();

    // assertFalse(boolValues.get(0)); // is admin
    assertTrue(message.isReadOnly()); // readonly
    assertFalse(message.isSinglePartition()); // single-part
    // assertFalse(boolValues.get(3)); // every site
    assertEquals("@AdHoc_RO_MP", message.getStoredProcedureName());

    byte[] serializedData = (byte[]) message.getStoredProcedureInvocation().getParameterAtIndex(0);
    AdHocPlannedStatement[] statements =
        AdHocPlannedStmtBatch.planArrayFromBuffer(ByteBuffer.wrap(serializedData));
    assertEquals(1, statements.length);
    String sql = new String(statements[0].sql, Constants.UTF8ENCODING);
    assertEquals("select * from a", sql);
  }
Exemplo n.º 2
0
  @Test
  public void testFinishedSPAdHocPlanning() throws Exception {
    // Need a batch and a statement
    AdHocPlannedStmtBatch plannedStmtBatch =
        new AdHocPlannedStmtBatch(
            "select * from a where i = 3",
            3,
            0,
            0,
            "localhost",
            false,
            ProcedureInvocationType.ORIGINAL,
            0,
            0,
            null);
    AdHocPlannedStatement s =
        new AdHocPlannedStatement(
            "select * from a where i = 3".getBytes(Constants.UTF8ENCODING),
            new CorePlan(
                new byte[0], null, new byte[20], null, false, false, true, new VoltType[0], 0),
            ParameterSet.fromArrayNoCopy(new Object[0]),
            null,
            null,
            3);
    plannedStmtBatch.addStatement(s);
    m_ci.processFinishedCompilerWork(plannedStmtBatch).run();

    ArgumentCaptor<Long> destinationCaptor = ArgumentCaptor.forClass(Long.class);
    ArgumentCaptor<Iv2InitiateTaskMessage> messageCaptor =
        ArgumentCaptor.forClass(Iv2InitiateTaskMessage.class);
    verify(m_messenger).send(destinationCaptor.capture(), messageCaptor.capture());
    Iv2InitiateTaskMessage message = messageCaptor.getValue();

    assertTrue(message.isReadOnly()); // readonly
    assertTrue(message.isSinglePartition()); // single-part
    assertEquals("@AdHoc_RO_SP", message.getStoredProcedureName());

    // SP AdHoc should have partitioning parameter serialized in the parameter set
    Object partitionParam = message.getStoredProcedureInvocation().getParameterAtIndex(0);
    assertTrue(partitionParam instanceof byte[]);
    VoltType type =
        VoltType.get((Byte) message.getStoredProcedureInvocation().getParameterAtIndex(1));
    assertTrue(type.isInteger());
    byte[] serializedData = (byte[]) message.getStoredProcedureInvocation().getParameterAtIndex(2);
    AdHocPlannedStatement[] statements =
        AdHocPlannedStmtBatch.planArrayFromBuffer(ByteBuffer.wrap(serializedData));
    assertTrue(Arrays.equals(TheHashinator.valueToBytes(3), (byte[]) partitionParam));
    assertEquals(1, statements.length);
    String sql = new String(statements[0].sql, Constants.UTF8ENCODING);
    assertEquals("select * from a where i = 3", sql);
  }
Exemplo n.º 3
0
  public void voltQueueSQL(final String sql, Object... args) {
    if (sql == null || sql.isEmpty()) {
      throw new IllegalArgumentException("SQL statement '" + sql + "' is null or the empty string");
    }

    try {
      AdHocPlannedStmtBatch paw =
          m_csp
              .plan(sql, !m_catProc.getSinglepartition(), ProcedureInvocationType.ORIGINAL, 0, 0)
              .get();
      if (paw.errorMsg != null) {
        throw new VoltAbortException("Failed to plan sql '" + sql + "' error: " + paw.errorMsg);
      }

      if (m_catProc.getReadonly() && !paw.isReadOnly()) {
        throw new VoltAbortException(
            "Attempted to queue DML adhoc sql '" + sql + "' from read only procedure");
      }

      assert (1 == paw.plannedStatements.size());

      QueuedSQL queuedSQL = new QueuedSQL();
      AdHocPlannedStatement plannedStatement = paw.plannedStatements.get(0);
      queuedSQL.stmt =
          SQLStmtAdHocHelper.createWithPlan(
              plannedStatement.sql,
              plannedStatement.core.aggregatorFragment,
              plannedStatement.core.collectorFragment,
              plannedStatement.core.isReplicatedTableDML,
              plannedStatement.core.readOnly,
              plannedStatement.core.parameterTypes);
      if (plannedStatement.extractedParamValues.size() == 0) {
        // case handles if there were parameters OR
        // if there were no constants to pull out
        queuedSQL.params = getCleanParams(queuedSQL.stmt, args);
      } else {
        if (args.length > 0) {
          throw new ExpectedProcedureException(
              "Number of arguments provided was "
                  + args.length
                  + " where 0 were expected for statement "
                  + sql);
        }
        Object[] extractedParams = plannedStatement.extractedParamValues.toArray();
        if (extractedParams.length != queuedSQL.stmt.numStatementParamJavaTypes) {
          String msg =
              String.format("Wrong number of extracted param for parameterized statement: %s", sql);
          throw new VoltAbortException(msg);
        }
        queuedSQL.params = getCleanParams(queuedSQL.stmt, extractedParams);
      }

      updateCRC(queuedSQL);
      m_batch.add(queuedSQL);
    } catch (Exception e) {
      if (e instanceof ExecutionException) {
        throw new VoltAbortException(e.getCause());
      }
      if (e instanceof VoltAbortException) {
        throw (VoltAbortException) e;
      }
      throw new VoltAbortException(e);
    }
  }