Search in sources :

Example 11 with OperatorContext

use of com.ibm.streams.operator.OperatorContext in project streamsx.kafka by IBMStreams.

the class AbstractKafkaProducerOperator method checkErrorPortSchema.

@ContextCheck(compile = true)
public static void checkErrorPortSchema(OperatorContextChecker checker) {
    final OperatorContext opCtx = checker.getOperatorContext();
    final int nOPorts = opCtx.getNumberOfStreamingOutputs();
    if (nOPorts == 0)
        return;
    StreamSchema inPortSchema = opCtx.getStreamingInputs().get(0).getStreamSchema();
    StreamSchema outSchema = opCtx.getStreamingOutputs().get(0).getStreamSchema();
    if (outSchema.getAttributeCount() > 2) {
        // $NON-NLS-1$
        checker.setInvalidContext(Messages.getString("PRODUCER_INVALID_OPORT_SCHEMA", opCtx.getKind()), new Object[0]);
    }
    // check attribute types
    int nTupleAttrs = 0;
    int nStringAttrs = 0;
    for (String outAttrName : outSchema.getAttributeNames()) {
        Attribute attr = outSchema.getAttribute(outAttrName);
        Type attrType = attr.getType();
        MetaType metaType = attrType.getMetaType();
        switch(metaType) {
            case TUPLE:
                ++nTupleAttrs;
                TupleType tupleType = (TupleType) attr.getType();
                StreamSchema tupleSchema = tupleType.getTupleSchema();
                if (!tupleSchema.equals(inPortSchema)) {
                    // $NON-NLS-1$
                    checker.setInvalidContext(Messages.getString("PRODUCER_INVALID_OPORT_SCHEMA", opCtx.getKind()), new Object[0]);
                }
                break;
            case RSTRING:
            case USTRING:
                ++nStringAttrs;
                break;
            case OPTIONAL:
                MetaType optionalValueMeta = ((OptionalType) attrType).getValueType().getMetaType();
                switch(optionalValueMeta) {
                    case RSTRING:
                    case USTRING:
                        ++nStringAttrs;
                        break;
                    default:
                        // $NON-NLS-1$
                        checker.setInvalidContext(Messages.getString("PRODUCER_INVALID_OPORT_SCHEMA", opCtx.getKind()), new Object[0]);
                }
                break;
            default:
                // $NON-NLS-1$
                checker.setInvalidContext(Messages.getString("PRODUCER_INVALID_OPORT_SCHEMA", opCtx.getKind()), new Object[0]);
        }
    }
    if (nTupleAttrs > 1 || nStringAttrs > 1)
        // $NON-NLS-1$
        checker.setInvalidContext(Messages.getString("PRODUCER_INVALID_OPORT_SCHEMA", opCtx.getKind()), new Object[0]);
}
Also used : MetaType(com.ibm.streams.operator.Type.MetaType) OptionalType(com.ibm.streams.operator.meta.OptionalType) TupleType(com.ibm.streams.operator.meta.TupleType) Type(com.ibm.streams.operator.Type) Attribute(com.ibm.streams.operator.Attribute) DefaultAttribute(com.ibm.streams.operator.model.DefaultAttribute) TupleAttribute(com.ibm.streams.operator.TupleAttribute) OperatorContext(com.ibm.streams.operator.OperatorContext) TupleType(com.ibm.streams.operator.meta.TupleType) StreamSchema(com.ibm.streams.operator.StreamSchema) Checkpoint(com.ibm.streams.operator.state.Checkpoint) MetaType(com.ibm.streams.operator.Type.MetaType) ContextCheck(com.ibm.streams.operator.OperatorContext.ContextCheck)

Example 12 with OperatorContext

use of com.ibm.streams.operator.OperatorContext in project streamsx.kafka by IBMStreams.

the class AbstractKafkaProducerOperator method checkDefaultKeyAttribute.

@ContextCheck(compile = true)
public static void checkDefaultKeyAttribute(OperatorContextChecker checker) {
    /*
         * When we specify a key attribute via `keyAttribute` parameter, the Streams compiler
         * checks for presence of the given attribute and for the right type.
         * When we do not specify the attribute, 'key' would be the default key attribute in the input schema.
         * We check the type of this default attribute if present.
         */
    final OperatorContext operatorContext = checker.getOperatorContext();
    if (!operatorContext.getParameterNames().contains(KEYATTR_PARAM_NAME)) {
        // no keyAttribute parameter - check presence of 'key' attribute in the schema
        StreamSchema schema = operatorContext.getStreamingInputs().get(0).getStreamSchema();
        // $NON-NLS-1$
        Attribute keyAttribute = schema.getAttribute(DEFAULT_KEY_ATTR_NAME);
        if (keyAttribute != null) {
            if (!checker.checkAttributeType(keyAttribute, SUPPORTED_ATTR_TYPES)) {
                final String msg = Messages.getString("UNSUPPORTED_ATTR_TYPE", operatorContext.getKind(), keyAttribute.getType().getLanguageType(), keyAttribute.getName());
                checker.setInvalidContext(msg, new Object[0]);
            }
        }
    }
}
Also used : Attribute(com.ibm.streams.operator.Attribute) DefaultAttribute(com.ibm.streams.operator.model.DefaultAttribute) TupleAttribute(com.ibm.streams.operator.TupleAttribute) OperatorContext(com.ibm.streams.operator.OperatorContext) StreamSchema(com.ibm.streams.operator.StreamSchema) ContextCheck(com.ibm.streams.operator.OperatorContext.ContextCheck)

Example 13 with OperatorContext

use of com.ibm.streams.operator.OperatorContext in project streamsx.kafka by IBMStreams.

the class AbstractKafkaProducerOperator method shutdown.

/**
 * Shutdown this operator, which will interrupt the thread executing the
 * <code>produceTuples()</code> method.
 *
 * @throws Exception
 *             Operator failure, will cause the enclosing PE to terminate.
 */
public synchronized void shutdown() throws Exception {
    OperatorContext context = getOperatorContext();
    Logger.getLogger(this.getClass()).trace(// $NON-NLS-1$ //$NON-NLS-2$
    "Operator " + context.getName() + " shutting down in PE: " + context.getPE().getPEId() + " in Job: " + // $NON-NLS-1$
    context.getPE().getJobId());
    producer.flush();
    producer.close(AbstractKafkaProducerClient.CLOSE_TIMEOUT_MS);
    if (this.errorPortSubmitter != null)
        this.errorPortSubmitter.stop();
    // Must call super.shutdown()
    super.shutdown();
}
Also used : OperatorContext(com.ibm.streams.operator.OperatorContext)

Example 14 with OperatorContext

use of com.ibm.streams.operator.OperatorContext in project streamsx.kafka by IBMStreams.

the class AbstractKafkaProducerOperator method checkDefaultMessageAttribute.

@ContextCheck(compile = true)
public static void checkDefaultMessageAttribute(OperatorContextChecker checker) {
    /*
         * When we specify a message attribute via `messageAttribute` parameter, the Streams compiler
         * checks for presence of the given attribute and for the right type.
         * When we do not specify the attribute, 'message' must be present in the input schema.
         * We can check presence and type of this default attribute.
         */
    final OperatorContext operatorContext = checker.getOperatorContext();
    if (!operatorContext.getParameterNames().contains(MESSAGEATTR_PARAM_NAME)) {
        // no messageAttribute parameter - check presence of 'message' attribute in the schema
        StreamSchema schema = operatorContext.getStreamingInputs().get(0).getStreamSchema();
        // $NON-NLS-1$
        Attribute messageAttribute = schema.getAttribute(DEFAULT_MESSAGE_ATTR_NAME);
        if (messageAttribute == null) {
            checker.setInvalidContext(Messages.getString("MESSAGE_ATTRIBUTE_NOT_FOUND"), new Object[0]);
            return;
        }
        if (!checker.checkAttributeType(messageAttribute, SUPPORTED_ATTR_TYPES)) {
            final String msg = Messages.getString("UNSUPPORTED_ATTR_TYPE", operatorContext.getKind(), messageAttribute.getType().getLanguageType(), messageAttribute.getName());
            checker.setInvalidContext(msg, new Object[0]);
        }
    }
}
Also used : Attribute(com.ibm.streams.operator.Attribute) DefaultAttribute(com.ibm.streams.operator.model.DefaultAttribute) TupleAttribute(com.ibm.streams.operator.TupleAttribute) OperatorContext(com.ibm.streams.operator.OperatorContext) StreamSchema(com.ibm.streams.operator.StreamSchema) ContextCheck(com.ibm.streams.operator.OperatorContext.ContextCheck)

Example 15 with OperatorContext

use of com.ibm.streams.operator.OperatorContext in project streamsx.topology by IBMStreams.

the class FunctionalOpUtils method checkNotConsistentRegionSource.

/**
 * Verify a functional operator is not the start of a consistent region.
 * @param checker Context checker.
 */
static void checkNotConsistentRegionSource(OperatorContextChecker checker) {
    OperatorContext context = checker.getOperatorContext();
    ConsistentRegionContext crc = context.getOptionalContext(ConsistentRegionContext.class);
    if (crc == null)
        return;
    if (crc.isStartOfRegion() || crc.isTriggerOperator())
        checker.setInvalidContext(Messages.getString("CONSISTENT_CHECK_1"), new String[] { context.getKind() });
}
Also used : ConsistentRegionContext(com.ibm.streams.operator.state.ConsistentRegionContext) OperatorContext(com.ibm.streams.operator.OperatorContext)

Aggregations

OperatorContext (com.ibm.streams.operator.OperatorContext)16 ContextCheck (com.ibm.streams.operator.OperatorContext.ContextCheck)7 Attribute (com.ibm.streams.operator.Attribute)4 StreamSchema (com.ibm.streams.operator.StreamSchema)4 TupleAttribute (com.ibm.streams.operator.TupleAttribute)4 DefaultAttribute (com.ibm.streams.operator.model.DefaultAttribute)4 ConsistentRegionContext (com.ibm.streams.operator.state.ConsistentRegionContext)4 RString (com.ibm.streams.operator.types.RString)3 StreamingInput (com.ibm.streams.operator.StreamingInput)1 Type (com.ibm.streams.operator.Type)1 MetaType (com.ibm.streams.operator.Type.MetaType)1 OptionalType (com.ibm.streams.operator.meta.OptionalType)1 TupleType (com.ibm.streams.operator.meta.TupleType)1 Checkpoint (com.ibm.streams.operator.state.Checkpoint)1 ConsumerClient (com.ibm.streamsx.kafka.clients.consumer.ConsumerClient)1 CrKafkaStaticAssignConsumerClient (com.ibm.streamsx.kafka.clients.consumer.CrKafkaStaticAssignConsumerClient)1 DummyConsumerClient (com.ibm.streamsx.kafka.clients.consumer.DummyConsumerClient)1 NonCrKafkaConsumerClient (com.ibm.streamsx.kafka.clients.consumer.NonCrKafkaConsumerClient)1