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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.catalina.tribes;
import java.io.Serializable;
import java.util.StringJoiner;
import java.util.concurrent.ScheduledExecutorService;
import org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
/**
* A channel is a representation of a group of nodes all participating in some sort of communication with each other.
* <p>
* The channel is the main API class for Tribes, this is essentially the only class that an application needs to be
* aware of. Through the channel the application can:
* <ul>
* <li>send messages</li>
* <li>receive message (by registering a <code>ChannelListener</code></li>
* <li>get all members of the group <code>getMembers()</code></li>
* <li>receive notifications of members added and members disappeared by registering a
* <code>MembershipListener</code></li>
* </ul>
* The channel has 5 major components:
* <ul>
* <li>Data receiver, with a built-in thread pool to receive messages from other peers</li>
* <li>Data sender, an implementation for sending data using NIO or java.io</li>
* <li>Membership listener,listens for membership broadcasts</li>
* <li>Membership broadcaster, broadcasts membership pings.</li>
* <li>Channel interceptors, the ability to manipulate messages as they are sent or arrive</li>
* </ul>
* The channel layout is:
*
* <pre>
* <code>
* ChannelListener_1..ChannelListener_N MembershipListener_1..MembershipListener_N [Application Layer]
* \ \ / /
* \ \ / /
* \ \ / /
* \ \ / /
* \ \ / /
* \ \ / /
* ---------------------------------------
* |
* |
* Channel
* |
* ChannelInterceptor_1
* | [Channel stack]
* ChannelInterceptor_N
* |
* Coordinator (implements MessageListener,MembershipListener,ChannelInterceptor)
* --------------------
* / | \
* / | \
* / | \
* / | \
* / | \
* MembershipService ChannelSender ChannelReceiver [IO layer]
* </code>
* </pre>
*
* @see org.apache.catalina.tribes.group.GroupChannel example usage
*/
public interface Channel {
/**
* Start and stop sequences can be controlled by these constants. This allows you to start separate components of
* the channel.
* <p>
* DEFAULT - starts or stops all components in the channel
*
* @see #start(int)
* @see #stop(int)
*/
int DEFAULT = 15;
/**
* Start and stop sequences can be controlled by these constants. This allows you to start separate components of
* the channel.
* <p>
* SND_RX_SEQ - starts or stops the data receiver. Start means opening a server socket in case of a TCP
* implementation
*
* @see #start(int)
* @see #stop(int)
*/
int SND_RX_SEQ = 1;
/**
* Start and stop sequences can be controlled by these constants. This allows you to start separate components of
* the channel.
* <p>
* SND_TX_SEQ - starts or stops the data sender. This should not open any sockets, as sockets are opened on demand
* when a message is being sent
*
* @see #start(int)
* @see #stop(int)
*/
int SND_TX_SEQ = 2;
/**
* Start and stop sequences can be controlled by these constants. This allows you to start separate components of
* the channel.
* <p>
* MBR_RX_SEQ - starts or stops the membership listener. In a multicast implementation this will open a datagram
* socket and join a group and listen for membership messages members joining
*
* @see #start(int)
* @see #stop(int)
*/
int MBR_RX_SEQ = 4;
/**
* Start and stop sequences can be controlled by these constants. This allows you to start separate components of
* the channel.
* <p>
* MBR_TX_SEQ - starts or stops the membership broadcaster. In a multicast implementation this will open a datagram
* socket and join a group and broadcast the local member information
*
* @see #start(int)
* @see #stop(int)
*/
int MBR_TX_SEQ = 8;
/**
* Send options, when a message is sent, it can have an option flag to trigger certain behavior. Most flags are used
* to trigger channel interceptors as the message passes through the channel stack.
* <p>
* However, there are five default flags that every channel implementation must implement.
* <p>
* SEND_OPTIONS_BYTE_MESSAGE - The message is a pure byte message and no marshaling or unmarshalling will be
* performed.
*
* @see #send(Member[], Serializable , int)
* @see #send(Member[], Serializable, int, ErrorHandler)
*/
int SEND_OPTIONS_BYTE_MESSAGE = 0x0001;
/**
* Send options, when a message is sent, it can have an option flag to trigger certain behavior. Most flags are used
* to trigger channel interceptors as the message passes through the channel stack.
* <p>
* However, there are five default flags that every channel implementation must implement
* <p>
* SEND_OPTIONS_USE_ACK - Message is sent and an ACK is received when the message has been received by the
* recipient. If no ack is received, the message is not considered successful.
*
* @see #send(Member[], Serializable , int)
* @see #send(Member[], Serializable, int, ErrorHandler)
*/
int SEND_OPTIONS_USE_ACK = 0x0002;
/**
* Send options, when a message is sent, it can have an option flag to trigger certain behavior. Most flags are used
* to trigger channel interceptors as the message passes through the channel stack.
* <p>
* However, there are five default flags that every channel implementation must implement
* <p>
* SEND_OPTIONS_SYNCHRONIZED_ACK - Message is sent and an ACK is received when the message has been received and
* processed by the recipient. If no ack is received, the message is not considered successful
*
* @see #send(Member[], Serializable , int)
* @see #send(Member[], Serializable, int, ErrorHandler)
*/
int SEND_OPTIONS_SYNCHRONIZED_ACK = 0x0004;
/**
* Send options, when a message is sent, it can have an option flag to trigger certain behavior. Most flags are used
* to trigger channel interceptors as the message passes through the channel stack.
* <p>
* However, there are five default flags that every channel implementation must implement
* <p>
* SEND_OPTIONS_ASYNCHRONOUS - Message will be placed on a queue and sent by a separate thread. If the queue is
* full, behaviour depends on {@link MessageDispatchInterceptor#isAlwaysSend()}
*
* @see #send(Member[], Serializable , int)
* @see #send(Member[], Serializable, int, ErrorHandler)
*/
int SEND_OPTIONS_ASYNCHRONOUS = 0x0008;
/**
* Send options, when a message is sent, it can have an option flag to trigger certain behavior. Most flags are used
* to trigger channel interceptors as the message passes through the channel stack.
* <p>
* However, there are five default flags that every channel implementation must implement
* <p>
* SEND_OPTIONS_SECURE - Message is sent over an encrypted channel
*
* @see #send(Member[], Serializable , int)
* @see #send(Member[], Serializable, int, ErrorHandler)
*/
int SEND_OPTIONS_SECURE = 0x0010;
/**
* Send options. When a message is sent with this flag on the system sends the message using UDP instead of TCP.
*
* @see #send(Member[], Serializable , int)
* @see #send(Member[], Serializable, int, ErrorHandler)
*/
int SEND_OPTIONS_UDP = 0x0020;
/**
* Send options. When a message is sent with this flag on the system sends a UDP message on the Multicast address
* instead of UDP or TCP to individual addresses.
*
* @see #send(Member[], Serializable , int)
* @see #send(Member[], Serializable, int, ErrorHandler)
*/
int SEND_OPTIONS_MULTICAST = 0x0040;
/**
* Send options, when a message is sent, it can have an option flag to trigger certain behavior. Most flags are used
* to trigger channel interceptors as the message passes through the channel stack.
* <p>
* However, there are five default flags that every channel implementation must implement
* <p>
* SEND_OPTIONS_DEFAULT - the default sending options, just a helper variable. The default is
* <code>SEND_OPTIONS_USE_ACK</code>
*
* @see #SEND_OPTIONS_USE_ACK
* @see #send(Member[], Serializable , int)
* @see #send(Member[], Serializable, int, ErrorHandler)
*/
int SEND_OPTIONS_DEFAULT = SEND_OPTIONS_USE_ACK;
/**
* Adds an interceptor to the stack for message processing. Interceptors are ordered in the way they are added.
*
* <pre>
* <code>
* channel.addInterceptor(A);
* channel.addInterceptor(C);
* channel.addInterceptor(B);
* </code>
* </pre>
*
* Will result in an interceptor stack like this: <code>A -> C -> B</code>
* <p>
* The complete stack will look like this: <code>Channel -> A -> C -> B -> ChannelCoordinator</code>
*
* @param interceptor ChannelInterceptorBase
*/
void addInterceptor(ChannelInterceptor interceptor);
/**
* Starts up the channel. This can be called multiple times for individual services to start The svc parameter can
* be the logical or value of any constants.
*
* @param svc one of:
* <ul>
* <li>DEFAULT - will start all services</li>
* <li>MBR_RX_SEQ - starts the membership receiver</li>
* <li>MBR_TX_SEQ - starts the membership broadcaster</li>
* <li>SND_TX_SEQ - starts the replication transmitter</li>
* <li>SND_RX_SEQ - starts the replication receiver</li>
* </ul>
* <b>Note:</b> In order for the membership broadcaster to transmit the correct information, it has
* to be started after the replication receiver.
*
* @throws ChannelException if a startup error occurs or the service is already started or an error occurs.
*/
void start(int svc) throws ChannelException;
/**
* Shuts down the channel. This can be called multiple times for individual services to shut down. The svc parameter
* can be the logical or value of any constants
*
* @param svc one of:
* <ul>
* <li>DEFAULT - will shut down all services</li>
* <li>MBR_RX_SEQ - stops the membership receiver</li>
* <li>MBR_TX_SEQ - stops the membership broadcaster</li>
* <li>SND_TX_SEQ - stops the replication transmitter</li>
* <li>SND_RX_SEQ - stops the replication receiver</li>
* </ul>
*
* @throws ChannelException if a startup error occurs or the service is already stopped or an error occurs.
*/
void stop(int svc) throws ChannelException;
/**
* Send a message to one or more members in the cluster
*
* @param destination Member[] - the destinations, cannot be null or zero length, the reason for that is that a
* membership change can occur and at that time the application is uncertain what group the
* message actually got sent to.
* @param msg Serializable - the message to send, has to be serializable, or a <code>ByteMessage</code> to
* send a pure byte array
* @param options int - sender options, see class documentation for each interceptor that is configured in order
* to trigger interceptors
*
* @return a unique Id that identifies the message that is sent
*
* @throws ChannelException if a serialization error happens.
*
* @see ByteMessage
* @see #SEND_OPTIONS_USE_ACK
* @see #SEND_OPTIONS_ASYNCHRONOUS
* @see #SEND_OPTIONS_SYNCHRONIZED_ACK
*/
UniqueId send(Member[] destination, Serializable msg, int options) throws ChannelException;
/**
* Send a message to one or more members in the cluster
*
* @param destination Member[] - the destinations, null or zero length means all
* @param msg ClusterMessage - the message to send
* @param options int - sender options, see class documentation
* @param handler ErrorHandler - handle errors through a callback, rather than throw it
*
* @return a unique Id that identifies the message that is sent
*
* @exception ChannelException - if a serialization error happens.
*/
UniqueId send(Member[] destination, Serializable msg, int options, ErrorHandler handler) throws ChannelException;
/**
* Sends a heart beat through the interceptor stacks. Use this method to alert interceptors and other components to
* clean up garbage, timed out messages etc.
* <p>
* If your application has a background thread, then you can save one thread, by configuring your channel to not use
* an internal heartbeat thread and invoking this method.
*
* @see #setHeartbeat(boolean)
*/
void heartbeat();
/**
* Enables or disables internal heartbeat.
*
* @param enable boolean - default value is implementation specific
*
* @see #heartbeat()
*/
void setHeartbeat(boolean enable);
/**
* Add a membership listener, will get notified when a new member joins, leaves or crashes.
* <p>
* If the membership listener implements the Heartbeat interface the <code>heartbeat()</code> method will be invoked
* when the heartbeat runs on the channel
*
* @param listener MembershipListener
*
* @see MembershipListener
*/
void addMembershipListener(MembershipListener listener);
/**
* Add a channel listener, this is a callback object when messages are received.
* <p>
* If the channel listener implements the Heartbeat interface the <code>heartbeat()</code> method will be invoked
* when the heartbeat runs on the channel
*
* @param listener ChannelListener
*
* @see ChannelListener
* @see Heartbeat
*/
void addChannelListener(ChannelListener listener);
/**
* Remove a membership listener, listeners are removed based on {@link Object#hashCode()} and
* {@link Object#equals(Object)}.
*
* @param listener MembershipListener
*
* @see MembershipListener
*/
void removeMembershipListener(MembershipListener listener);
/**
* Remove a channel listener, listeners are removed based on {@link Object#hashCode()} and
* {@link Object#equals(Object)}.
*
* @param listener ChannelListener
*
* @see ChannelListener
*/
void removeChannelListener(ChannelListener listener);
/**
* Returns true if there are any members in the group. This call is the same as
* <code>getMembers().length > 0</code>
*
* @return boolean - true if there are any members automatically discovered
*/
boolean hasMembers();
/**
* Get all current group members.
*
* @return all members or empty array, never null
*/
Member[] getMembers();
/**
* Return the member that represents this node. This is also the data that gets broadcast through the membership
* broadcaster component
*
* @param incAlive - optimization, true if you want it to calculate alive time since the membership service started.
*
* @return Member
*/
Member getLocalMember(boolean incAlive);
/**
* Returns the member from the membership service with complete and recent data. Some implementations might
* serialize and send membership information along with a message, and instead of sending complete membership
* details, only send the primary identifier for the member but not the payload or other information. When such
* message is received the application can retrieve the cached member through this call. In most cases, this is not
* necessary.
*
* @param mbr Member
*
* @return Member
*/
Member getMember(Member mbr);
/**
* Return the name of this channel.
*
* @return channel name
*/
String getName();
/**
* Set the name of this channel
*
* @param name The new channel name
*/
void setName(String name);
/**
* Return executor that can be used for utility tasks.
*
* @return the executor
*/
ScheduledExecutorService getUtilityExecutor();
/**
* Set the executor that can be used for utility tasks.
*
* @param utilityExecutor the executor
*/
void setUtilityExecutor(ScheduledExecutorService utilityExecutor);
/**
* Translates the name of an option to its integer value. Valid option names are "asynchronous" (alias "async"),
* "byte_message" (alias "byte"), "multicast", "secure", "synchronized_ack" (alias "sync"), "udp", "use_ack"
*
* @param opt The name of the option
*
* @return the int value of the passed option name
*/
static int getSendOptionValue(String opt) {
return switch (opt) {
case "asynchronous", "async" -> SEND_OPTIONS_ASYNCHRONOUS;
case "byte_message", "byte" -> SEND_OPTIONS_BYTE_MESSAGE;
case "multicast" -> SEND_OPTIONS_MULTICAST;
case "secure" -> SEND_OPTIONS_SECURE;
case "synchronized_ack", "sync" -> SEND_OPTIONS_SYNCHRONIZED_ACK;
case "udp" -> SEND_OPTIONS_UDP;
case "use_ack" -> SEND_OPTIONS_USE_ACK;
default -> throw new IllegalArgumentException(String.format("[%s] is not a valid option", opt));
};
}
/**
* Translates a comma separated list of option names to their bitwise-ORd value
*
* @param input A comma separated list of options, e.g. "async, multicast"
*
* @return a bitwise ORd value of the passed option names
*/
static int parseSendOptions(String input) {
try {
return Integer.parseInt(input);
} catch (NumberFormatException nfe) {
final Log log = LogFactory.getLog(Channel.class);
if (log.isTraceEnabled()) {
log.trace(String.format("Failed to parse [%s] as integer, channelSendOptions possibly set by name(s)",
input), nfe);
}
}
String[] options = input.split("\\s*,\\s*");
int result = 0;
for (String opt : options) {
result |= getSendOptionValue(opt);
}
return result;
}
/**
* Translates an integer value of SendOptions to its human-friendly comma separated value list for use in JMX and
* such.
*
* @param input the int value of SendOptions
*
* @return the human-friendly string representation in a reverse order (i.e. the last option will be shown first)
*/
static String getSendOptionsAsString(int input) {
// allOptionNames must be in order of the bits of the available options
final String[] allOptionNames =
new String[] { "byte", "use_ack", "sync", "async", "secure", "udp", "multicast" };
StringJoiner names = new StringJoiner(", ");
for (int bit = allOptionNames.length - 1; bit >= 0; bit--) {
// if the bit is set then add the name to the result
if (((1 << bit) & input) > 0) {
names.add(allOptionNames[bit]);
}
}
return names.toString();
}
}
|