-
Notifications
You must be signed in to change notification settings - Fork 68
/
AmqpMsgSendOpDispenser.java
207 lines (178 loc) · 8.33 KB
/
AmqpMsgSendOpDispenser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2022-2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.adapter.amqp.dispensers;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import io.nosqlbench.adapter.amqp.AmqpSpace;
import io.nosqlbench.adapter.amqp.exception.AmqpAdapterInvalidParamException;
import io.nosqlbench.adapter.amqp.exception.AmqpAdapterUnexpectedException;
import io.nosqlbench.adapter.amqp.ops.OpTimeTrackAmqpMsgSendOp;
import io.nosqlbench.adapter.amqp.ops.AmqpTimeTrackOp;
import io.nosqlbench.adapter.amqp.util.AmqpAdapterUtil;
import io.nosqlbench.adapters.api.activityimpl.uniform.DriverAdapter;
import io.nosqlbench.adapters.api.templating.ParsedOp;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.util.function.LongFunction;
import java.util.function.Predicate;
public class AmqpMsgSendOpDispenser extends AmqpBaseOpDispenser {
private final static Logger logger = LogManager.getLogger(AmqpMsgSendOpDispenser.class);
private final boolean publisherConfirm ;
// Only relevant when 'publisherConfirm' is true
// - default to "individual" confirm
private final String confirmMode;
// Only relevant when 'publisherConfirm' is true and 'confirmMode' is 'batch'
// - default to 100
private int confirmBatchNum;
private final LongFunction<String> routingKeyFunc;
private final LongFunction<String> msgPayloadFunc;
public AmqpMsgSendOpDispenser(DriverAdapter adapter,
ParsedOp op,
AmqpSpace amqpSpace) {
super(adapter, op, amqpSpace);
publisherConfirm = parsedOp
.getOptionalStaticConfig("publisher_confirm", String.class)
.filter(Predicate.not(String::isEmpty))
.map(BooleanUtils::toBoolean)
.orElse(false);
confirmMode = parsedOp
.getOptionalStaticValue("confirm_mode", String.class)
.orElse(AmqpAdapterUtil.AMQP_PUB_CONFIRM_MODE.INDIVIDUAL.label);
if (! AmqpAdapterUtil.AMQP_PUB_CONFIRM_MODE.isValidLabel(confirmMode)) {
throw new AmqpAdapterInvalidParamException("confirm_mode",
"The provided value \"" + confirmMode + "\" is not one of following valid values: '" +
AmqpAdapterUtil.getValidAmqpPublisherConfirmModeList() + "'");
}
confirmBatchNum = parsedOp
.getOptionalStaticConfig("confirm_batch_num", String.class)
.filter(Predicate.not(String::isEmpty))
.map(NumberUtils::toInt)
.orElse(AmqpAdapterUtil.DFT_AMQP_PUBLISH_CONFIRM_BATCH_NUM);
if (confirmBatchNum < AmqpAdapterUtil.AMQP_PUBLISH_CONFIRM_BATCH_NUM_MIN) {
confirmBatchNum = AmqpAdapterUtil.DFT_AMQP_PUBLISH_CONFIRM_BATCH_NUM;
}
routingKeyFunc = lookupOptionalStrOpValueFunc("routing_key", null);
msgPayloadFunc = lookupMandtoryStrOpValueFunc("message");
}
private long getExchangeSenderSeqNum(long cycle) {
return (cycle / ((long) amqpSpace.getAmqpConnNum() *
amqpSpace.getAmqpConnChannelNum() *
amqpSpace.getAmqpChannelExchangeNum())
) % amqpSpace.getAmqpMsgClntNum();
}
private String getEffectiveSenderNameByCycle(long cycle) {
return getEffectiveSenderNameByCycle(
getConnSeqNum(cycle),
getConnChannelSeqNum(cycle),
getChannelExchangeSeqNum(cycle),
getExchangeSenderSeqNum(cycle));
}
private String getEffectiveSenderNameByCycle(long connSeqNum,
long channelSeqNum,
long exchangeSeqNum,
long senderSeqNum) {
return String.format(
"sender-%d-%d-%d-%d",
connSeqNum,
channelSeqNum,
exchangeSeqNum,
senderSeqNum);
}
private Channel getAmqpChannelForSender(long cycle) {
long connSeqNum = getConnSeqNum(cycle);
long channelSeqNum = getConnChannelSeqNum(cycle);
Connection amqpConnection = amqpSpace.getAmqpConnection(connSeqNum);
AmqpSpace.AmqpChannelKey senderKey = new AmqpSpace.AmqpChannelKey(connSeqNum, channelSeqNum);
return amqpSpace.getAmqpChannels(senderKey, () -> {
Channel channel = null;
try {
channel = amqpConnection.createChannel();
if (logger.isDebugEnabled()) {
logger.debug("Created channel for amqp connection: {}, channel: {}",
amqpConnection, channel);
}
}
catch (IOException ex) {
// Do not throw exception here, just log it and return null
if (logger.isDebugEnabled()) {
logger.debug("Failed to create channel for amqp connection: " + amqpConnection, ex);
}
}
try {
if ((channel != null) && publisherConfirm) {
channel.confirmSelect();
if (StringUtils.equalsIgnoreCase(confirmMode, AmqpAdapterUtil.AMQP_PUB_CONFIRM_MODE.ASYNC.label)) {
channel.addConfirmListener((sequenceNumber, multiple) -> {
// code when message is confirmed
if (logger.isTraceEnabled()) {
logger.debug("Async ack received for a published message: {}, {}",
sequenceNumber, multiple);
}
}, (sequenceNumber, multiple) -> {
// code when message is nack-ed
if (logger.isTraceEnabled()) {
logger.debug("Async n-ack received of a published message: {}, {}",
sequenceNumber, multiple);
}
});
}
if (logger.isTraceEnabled()) {
logger.debug("Publisher Confirms is enabled on AMQP channel: {}({}), {}",
confirmMode,
confirmBatchNum,
channel);
}
}
} catch (IOException ex) {
throw new AmqpAdapterUnexpectedException(
"Failed to enable publisher acknowledgement on the AMQP channel (" +
channel + ")!");
}
return channel;
});
}
@Override
public AmqpTimeTrackOp getOp(long cycle) {
String msgPayload = msgPayloadFunc.apply(cycle);
if (StringUtils.isBlank(msgPayload)) {
throw new AmqpAdapterInvalidParamException("Message payload must be specified and can't be empty!");
}
Channel channel = getAmqpChannelForSender(cycle);
if (channel == null) {
throw new AmqpAdapterUnexpectedException(
String.format(
"Failed to get AMQP channel for sender %s [%d]!",
getEffectiveSenderNameByCycle(cycle),
cycle));
}
String exchangeName = getEffectiveExchangeNameByCycle(cycle);
declareExchange(channel, exchangeName, amqpSpace.getAmqpExchangeType());
return new OpTimeTrackAmqpMsgSendOp(
amqpAdapterMetrics,
amqpSpace,
channel,
exchangeName,
msgPayload,
routingKeyFunc.apply(cycle),
publisherConfirm,
confirmMode,
confirmBatchNum);
}
}