mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
wip
This commit is contained in:
42
nats_admin/signals.md
Normal file
42
nats_admin/signals.md
Normal file
@@ -0,0 +1,42 @@
|
||||
## Process Signaling
|
||||
|
||||
On Unix systems, the NATS server responds to the following signals:
|
||||
|
||||
| Signal | Result |
|
||||
| :--- | :--- |
|
||||
| SIGKILL | Kills the process immediately |
|
||||
| SIGINT | Stops the server gracefully |
|
||||
| SIGUSR1 | Reopens the log file for log rotation |
|
||||
| SIGHUP | Reloads server configuration file |
|
||||
| SIGUSR2 | Stops the server after evicting all clients (lame duck mode) |
|
||||
|
||||
The `nats-server` binary can be used to send these signals to running NATS servers using the `-sl` flag:
|
||||
|
||||
```sh
|
||||
# Quit the server
|
||||
nats-server -sl quit
|
||||
|
||||
# Stop the server
|
||||
nats-server -sl stop
|
||||
|
||||
# Reopen log file for log rotation
|
||||
nats-server -sl reopen
|
||||
|
||||
# Reload server configuration
|
||||
nats-server -sl reload
|
||||
|
||||
# Lame duck mode server configuration
|
||||
nats-server -sl ldm
|
||||
```
|
||||
|
||||
If there are multiple `nats-server` processes running, or if `pgrep` isn't available, you must either specify a PID or the absolute path to a PID file:
|
||||
|
||||
```sh
|
||||
nats-server -sl stop=<pid>
|
||||
```
|
||||
|
||||
```sh
|
||||
nats-server -sl stop=/path/to/pidfile
|
||||
```
|
||||
|
||||
See the [Windows Service](windows_srv.md) section for information on signaling the NATS server on Windows.
|
||||
113
nats_admin/slow_consumers.md
Normal file
113
nats_admin/slow_consumers.md
Normal file
@@ -0,0 +1,113 @@
|
||||
## Slow Consumers
|
||||
|
||||
To support resiliency and high availability, NATS provides built-in mechanisms to automatically prune the registered listener interest graph that is used to keep track of subscribers, including slow consumers and lazy listeners. NATS automatically handles a slow consumer. If a client is not processing messages quick enough, the NATS server cuts it off. To support scaling, NATS provides for auto-pruning of client connections. If a subscriber does not respond to ping requests from the server within the [ping-pong interval](/documentation/internals/nats-protocol/), the client is cut off (disconnected). The client will need to have reconnect logic to reconnect with the server.
|
||||
|
||||
# Slow Consumers
|
||||
|
||||
In core NATS, consumers that cannot keep up are handled differently from many other messaging systems: NATS favors the approach of protecting the system as a whole over accommodating a particular consumer to ensure message delivery.
|
||||
|
||||
__What is a slow consumer?__
|
||||
|
||||
A slow consumer is a subscriber that cannot keep up with the message flow delivered from the NATS server. This is a common case in distributed systems because it is often easier to generate data than it is to process it. When consumers cannot process data fast enough, back pressure is applied to the rest of the system. NATS has mechanisms to reduce this back pressure.
|
||||
|
||||
NATS identifies slow consumers in the client or the server, providing notification through registered callbacks, log messages, and statistics in the server's monitoring endpoints.
|
||||
|
||||
__What happens to slow consumers?__
|
||||
|
||||
When detected at the client, the application is notified and messages are dropped to allow the consumer to continue and reduce potential back pressure. When detected in the server, the server will disconnect the connection with the slow consumer to protect itself and the integrity of the messaging system.
|
||||
|
||||
## Slow consumers identified in the client
|
||||
|
||||
A [client can detect it is a slow consumer](/documentation/writing_applications/advanced#slow-consumers) on a local connection and notify the application through use of the asynchronous error callback. It is better to catch a slow consumer locally in the client rather than to allow the server to detect this condition. This example demonstrates how to define and register an asynchronous error handler that will handle slow consumer errors.
|
||||
|
||||
```go
|
||||
func natsErrHandler(nc *nats.Conn, sub *nats.Subscription, natsErr error) {
|
||||
fmt.Printf("error: %v\n", natsErr)
|
||||
if natsErr == nats.ErrSlowConsumer {
|
||||
pendingMsgs, _, err := sub.Pending()
|
||||
if err != nil {
|
||||
fmt.Printf("couldn't get pending messages: %v", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Falling behind with %d pending messages on subject %q.\n",
|
||||
pendingMsgs, sub.Subject)
|
||||
// Log error, notify operations...
|
||||
}
|
||||
// check for other errors
|
||||
}
|
||||
|
||||
// Set the error handler when creating a connection.
|
||||
nc, err := nats.Connect("nats://localhost:4222",
|
||||
nats.ErrorHandler(natsErrHandler))
|
||||
```
|
||||
|
||||
With this example code and default settings, a slow consumer error would generate output something like this:
|
||||
|
||||
```sh
|
||||
error: nats: slow consumer, messages dropped
|
||||
Falling behind with 65536 pending messages on subject "foo".
|
||||
```
|
||||
|
||||
Note that if you are using a synchronous subscriber, `Subscription.NextMsg(timeout time.Duration)` will also return an error indicating there was a slow consumer and messages have been dropped.
|
||||
|
||||
## Slow consumers identified by the server
|
||||
|
||||
When a client does not process messages fast enough, the server will buffer messages in the outbound connection to the client. When this happens and the server cannot write data fast enough to the client, in order to protect itself, it will designate a subscriber as a "slow consumer" and may drop the associated connection.
|
||||
|
||||
When the server initiates a slow consumer error, you'll see the following in the server output:
|
||||
|
||||
```sh
|
||||
[54083] 2017/09/28 14:45:18.001357 [INF] ::1:63283 - cid:7 - Slow Consumer Detected
|
||||
```
|
||||
|
||||
The server will also keep count of the number of slow consumer errors encountered, available through the monitoring `varz` endpoint in the `slow_consumers` field.
|
||||
|
||||
## Handling slow consumers
|
||||
|
||||
Apart from using [NATS streaming](http://nats.io/doc/streaming/nats-streaming-intro/) or optimizing your consuming application, there are a few options available: scale, meter, or tune NATS to your environment.
|
||||
|
||||
__Scaling with queue subscribers__
|
||||
|
||||
This is ideal if you do not rely on message order. Ensure your NATS subscription belongs to a [queue group](http://nats.io/documentation/concepts/nats-queueing/), then scale as required by creating more instances of your service or application. This is a great approach for microservices - each instance of your microservice will receive a portion of the messages to process, and simply add more instances of your service to scale. No code changes, configuration changes, or downtime whatsoever.
|
||||
|
||||
__Create a subject namespace that can scale__
|
||||
|
||||
You can distribute work further through the subject namespace, with some forethought in design. This approach is useful if you need to preserve message order. The general idea is to publish to a deep subject namespace, and consume with wildcard subscriptions while giving yourself room to expand and distribute work in the future.
|
||||
|
||||
For a simple example, if you have a service that receives telemetry data from IoT devices located throughout a city, you can publish to a subject namespace like `Sensors.North`, `Sensors.South`, `Sensors.East` and `Sensors.West`. Initially, you'll subscribe to `Sensors.>` to process everything in one consumer. As your enterprise grows and data rates exceed what one consumer can handle, you can replace your single consumer with four consuming applications to subscribe to each subject representing a smaller segment of your data. Note that your publishing applications remain untouched.
|
||||
|
||||
__Meter the publisher__
|
||||
|
||||
A less favorable option may be to meter the publisher. There are several ways to do this varying from simply slowing down your publisher to a more complex approach periodically issuing a blocking request/reply to match subscriber rates.
|
||||
|
||||
__Tune NATS through configuration__
|
||||
|
||||
The NATS server can be tuned to determine how much data can be buffered before a consumer is considered slow, and some officially supported clients allow buffer sizes to be adjusted. Decreasing buffer sizes will let you identify slow consumers more quickly. Increasing buffer sizes is not typically recommended unless you are handling temporary bursts of data. Often, increasing buffer capacity will only *postpone* slow consumer problems.
|
||||
|
||||
### Server Configuration
|
||||
|
||||
The NATS server has a write deadline it uses to write to a connection. When this write deadline is exceeded, a client is considered to have a slow consumer. If you are encountering slow consumer errors in the server, you can increase the write deadline to buffer more data.
|
||||
|
||||
The `write_deadline` configuration option in the NATS server configuration file will tune this:
|
||||
|
||||
```ascii
|
||||
write_deadline: 2s
|
||||
```
|
||||
|
||||
Tuning this parameter is ideal when you have bursts of data to accommodate. **_Be sure you are not just postponing a slow consumer error._**
|
||||
|
||||
### Client Configuration
|
||||
|
||||
Most officially supported clients have an internal buffer of pending messages and will notify your application through an asynchronous error callback if a local subscription is not catching up. Receiving an error locally does not necessarily mean that the server will have identified a subscription as a slow consumer.
|
||||
|
||||
This buffer can be configured through setting the pending limits after a subscription has been created:
|
||||
|
||||
```go
|
||||
if err := sub.SetPendingLimits(1024*500, 1024*5000); err != nil {
|
||||
log.Fatalf("Unable to set pending limits: %v", err)
|
||||
}
|
||||
```
|
||||
|
||||
The default subscriber pending message limit is `65536`, and the default subscriber pending byte limit is `65536*1024`
|
||||
|
||||
If the client reaches this internal limit, it will drop messages and continue to process new messages. This is aligned with NATS at most once delivery. It is up to your application to detect the missing messages and recover from this condition.
|
||||
86
nats_admin/upgrading_cluster.md
Normal file
86
nats_admin/upgrading_cluster.md
Normal file
@@ -0,0 +1,86 @@
|
||||
## Cluster Upgrading
|
||||
|
||||
The basic strategy for upgrading a cluster revolves around the server's ability to gossip cluster configuration to clients and other servers. When cluster configuration changes, clients become aware of new servers automatically. In case of a disconnect, a client has a list of servers that joined the cluster in addition to the ones it knew about from its connection settings.
|
||||
|
||||
Note that since each server stores it's own permission and authentication configuration, new servers added to a cluster should provide the same users and authorization to prevent clients from getting rejected or gaining unexpected privileges.
|
||||
|
||||
For purposes of describing the scenario, let's get some fingers on keyboards, and go through the motions. Let's consider a cluster of two servers: 'A' and 'B.', and yes - clusters should be *three* to *five* servers, but for purposes of describing the behavior and cluster upgrade process, a cluster of two servers will suffice.
|
||||
|
||||
Let's build this cluster:
|
||||
|
||||
```bash
|
||||
nats-server -D -p 4222 -cluster nats://localhost:6222 -routes nats://localhost:6222,nats://localhost:6333
|
||||
```
|
||||
|
||||
The command above is starting nats-server with debug output enabled, listening for clients on port 4222, and accepting cluster connections on port 6222. The `-routes` option specifies a list of nats URLs where the server will attempt to connect
|
||||
to other servers. These URLs define the cluster ports enabled on the cluster peers.
|
||||
|
||||
Keen readers will notice a self-route. Gnatsd will ignore the self-route, but it makes for a single consistent configuration for all servers.
|
||||
|
||||
You will see the server started, we notice it emits some warnings because it cannot connect to 'localhost:6333'. The message more accurately reads:
|
||||
|
||||
```ascii
|
||||
Error trying to connect to route: dial tcp localhost:6333: connect: connection refused
|
||||
```
|
||||
|
||||
Let's fix that, by starting the second server:
|
||||
```bash
|
||||
nats-server -D -p 4333 -cluster nats://localhost:6333 -routes nats://localhost:6222,nats://localhost:6333
|
||||
```
|
||||
The second server was started on port 4333 with its cluster port on 6333. Otherwise the same as 'A.'
|
||||
|
||||
Let's get one client, so we can observe it moving between servers as servers get removed:
|
||||
```bash
|
||||
nats-sub -s nats://localhost:4222 ">"
|
||||
```
|
||||
|
||||
Nats-sub is a subscriber sample included with all NATS clients. Nats-sub subscribes to a subject and prints out any messages received. You can find the source code to the go version of nats-sub [here)(https://github.com/nats-io/go-nats/tree/master/examples). After starting the subscriber you should see a message on 'A' that a new client connected.
|
||||
|
||||
We have two servers and a client. Time to simulate our rolling upgrade. But wait, before we upgrade 'A,' let's introduce a new server 'T.' Server 'T' will join the existing cluster while we perform the upgrade. Its sole purpose is to provide an additional place where clients can go besides 'A.' and ensure we don't end up with a single server serving all the clients after the upgrade procedure. Clients will randomly select a server when connecting unless a special option is provided that disables that functionality (usually called 'DontRandomize' or 'noRandomize'). You can read more about ["Avoiding the Thundering Herd"](https://www.nats.io/documentation/writing_applications/connecting/).
|
||||
Suffice it to say that clients redistribute themselves about evenly between all servers in the cluster. In our case 1/2 of the clients on 'A' will jump over to 'B' and the remaining half to 'T.'
|
||||
|
||||
Let's start our temporary server:
|
||||
|
||||
```bash
|
||||
nats-server -D -p 4444 -cluster nats://localhost:6444 -routes nats://localhost:6222,nats://localhost:6333
|
||||
```
|
||||
|
||||
After an instant or so, clients on 'A' learn of the new cluster member that joined. On our hands-on tutorial, `nats-sub` is now aware of 3 possible servers, 'A' (specified when we started the tool) and 'B' and 'T' learned from the cluster gossip.
|
||||
|
||||
We invoke our admin powers and turn off 'A' by issuing a `CTRL+C` to the terminal on 'A,' and observe that either 'B' or 'T' reports that a new client connected. That is our `nats-sub` client.
|
||||
|
||||
We perform the upgrade process, update the binary for 'A', and restart 'A':
|
||||
|
||||
```bash
|
||||
nats-server -D -p 4222 -cluster nats://localhost:6222 -routes nats://localhost:6222,nats://localhost:6333
|
||||
```
|
||||
|
||||
We move on to upgrade 'B'. Notice that clients from 'B' reconnect to 'A' and 'T'. We upgrade and restart 'B':
|
||||
|
||||
```bash
|
||||
nats-server -D -p 4333 -cluster nats://localhost:6333 -routes nats://localhost:6222,nats://localhost:6333
|
||||
```
|
||||
|
||||
If we had more servers, we would continue the stop, update, restart rotation as we did for 'A' and 'B.' After restarting the last server, we can go ahead and turn off 'T.' Any clients on 'T' will redistribute to our permanent cluster members.
|
||||
|
||||
|
||||
### Seed Servers
|
||||
|
||||
In the examples above we started nats-server specifying two clustering routes. It is possible to allow the server gossip protocol drive it and reduce the amount of configuration. You could for example start A, B and C as follows:
|
||||
|
||||
#### A - Seed Server
|
||||
```bash
|
||||
nats-server -D -p 4222 -cluster nats://localhost:6222
|
||||
```
|
||||
|
||||
#### B
|
||||
```bash
|
||||
nats-server -D -p 4333 -cluster nats://localhost:6333 -routes nats://localhost:6222
|
||||
```
|
||||
|
||||
#### C
|
||||
```bash
|
||||
nats-server -D -p 4444 -cluster nats://localhost:6444 -routes nats://localhost:6222
|
||||
```
|
||||
|
||||
Once they connect to the 'seed server', the will learn about all the other servers and connect to each other forming the full mesh.
|
||||
Reference in New Issue
Block a user