Search in sources :

Example 1 with CompressionKind

use of org.apache.atlas.model.notification.AtlasNotificationBaseMessage.CompressionKind in project atlas by apache.

the class AbstractNotification method createNotificationMessages.

/**
 * Get the notification message JSON from the given object.
 *
 * @param message  the message in object form
 *
 * @return the message as a JSON string
 */
public static void createNotificationMessages(Object message, List<String> msgJsonList) {
    AtlasNotificationMessage<?> notificationMsg = new AtlasNotificationMessage<>(CURRENT_MESSAGE_VERSION, message, getHostAddress(), getCurrentUser());
    String msgJson = AtlasType.toV1Json(notificationMsg);
    boolean msgLengthExceedsLimit = (msgJson.length() * MAX_BYTES_PER_CHAR) > MESSAGE_MAX_LENGTH_BYTES;
    if (msgLengthExceedsLimit) {
        // get utf-8 bytes for msgJson and check for length limit again
        byte[] msgBytes = AtlasNotificationBaseMessage.getBytesUtf8(msgJson);
        msgLengthExceedsLimit = msgBytes.length > MESSAGE_MAX_LENGTH_BYTES;
        if (msgLengthExceedsLimit) {
            String msgId = getNextMessageId();
            CompressionKind compressionKind = CompressionKind.NONE;
            if (MESSAGE_COMPRESSION_ENABLED) {
                byte[] encodedBytes = AtlasNotificationBaseMessage.gzipCompressAndEncodeBase64(msgBytes);
                compressionKind = CompressionKind.GZIP;
                LOG.info("Compressed large message: msgID={}, uncompressed={} bytes, compressed={} bytes", msgId, msgBytes.length, encodedBytes.length);
                msgLengthExceedsLimit = encodedBytes.length > MESSAGE_MAX_LENGTH_BYTES;
                if (!msgLengthExceedsLimit) {
                    // no need to split
                    AtlasNotificationStringMessage compressedMsg = new AtlasNotificationStringMessage(encodedBytes, msgId, compressionKind);
                    // msgJson will not have multi-byte characters here, due to use of encodeBase64() above
                    msgJson = AtlasType.toV1Json(compressedMsg);
                    // not used after this point
                    msgBytes = null;
                } else {
                    // encodedBytes will be split
                    // not used after this point
                    msgJson = null;
                    msgBytes = encodedBytes;
                }
            }
            if (msgLengthExceedsLimit) {
                // compressed messages are already base64-encoded
                byte[] encodedBytes = MESSAGE_COMPRESSION_ENABLED ? msgBytes : AtlasNotificationBaseMessage.encodeBase64(msgBytes);
                int splitCount = encodedBytes.length / MESSAGE_MAX_LENGTH_BYTES;
                if ((encodedBytes.length % MESSAGE_MAX_LENGTH_BYTES) != 0) {
                    splitCount++;
                }
                for (int i = 0, offset = 0; i < splitCount; i++) {
                    int length = MESSAGE_MAX_LENGTH_BYTES;
                    if ((offset + length) > encodedBytes.length) {
                        length = encodedBytes.length - offset;
                    }
                    AtlasNotificationStringMessage splitMsg = new AtlasNotificationStringMessage(encodedBytes, offset, length, msgId, compressionKind, i, splitCount);
                    String splitMsgJson = AtlasType.toV1Json(splitMsg);
                    msgJsonList.add(splitMsgJson);
                    offset += length;
                }
                LOG.info("Split large message: msgID={}, splitCount={}, length={} bytes", msgId, splitCount, encodedBytes.length);
            }
        }
    }
    if (!msgLengthExceedsLimit) {
        msgJsonList.add(msgJson);
    }
}
Also used : CompressionKind(org.apache.atlas.model.notification.AtlasNotificationBaseMessage.CompressionKind) AtlasNotificationMessage(org.apache.atlas.model.notification.AtlasNotificationMessage) AtlasNotificationStringMessage(org.apache.atlas.model.notification.AtlasNotificationStringMessage)

Aggregations

CompressionKind (org.apache.atlas.model.notification.AtlasNotificationBaseMessage.CompressionKind)1 AtlasNotificationMessage (org.apache.atlas.model.notification.AtlasNotificationMessage)1 AtlasNotificationStringMessage (org.apache.atlas.model.notification.AtlasNotificationStringMessage)1