|
| 1 | +package queue_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/tarantool/go-tarantool" |
| 10 | + "github.com/tarantool/go-tarantool/connection_pool" |
| 11 | + "github.com/tarantool/go-tarantool/queue" |
| 12 | + "github.com/tarantool/go-tarantool/test_helpers" |
| 13 | +) |
| 14 | + |
| 15 | +// QueueConnectionHandler handles new connections in a ConnectionPool. |
| 16 | +type QueueConnectionHandler struct { |
| 17 | + name string |
| 18 | + cfg queue.Cfg |
| 19 | + |
| 20 | + uuid uuid.UUID |
| 21 | + registered bool |
| 22 | + err error |
| 23 | + mutex sync.Mutex |
| 24 | + masterUpdated chan struct{} |
| 25 | +} |
| 26 | + |
| 27 | +// QueueConnectionHandler implements the ConnectionHandler interface. |
| 28 | +var _ connection_pool.ConnectionHandler = &QueueConnectionHandler{} |
| 29 | + |
| 30 | +// NewQueueConnectionHandler creates a QueueConnectionHandler object. |
| 31 | +func NewQueueConnectionHandler(name string, cfg queue.Cfg) *QueueConnectionHandler { |
| 32 | + return &QueueConnectionHandler{ |
| 33 | + name: name, |
| 34 | + cfg: cfg, |
| 35 | + masterUpdated: make(chan struct{}, 10), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Discovered configures a queue for an instance and identifies a shared queue |
| 40 | +// session on master instances. |
| 41 | +// |
| 42 | +// NOTE: the Queue supports only a master-replica cluster configuration. It |
| 43 | +// does not support a master-master configuration. |
| 44 | +func (h *QueueConnectionHandler) Discovered(conn *tarantool.Connection, |
| 45 | + role connection_pool.Role) error { |
| 46 | + h.mutex.Lock() |
| 47 | + defer h.mutex.Unlock() |
| 48 | + |
| 49 | + if h.err != nil { |
| 50 | + return h.err |
| 51 | + } |
| 52 | + |
| 53 | + master := role == connection_pool.MasterRole |
| 54 | + if master { |
| 55 | + defer func() { |
| 56 | + h.masterUpdated <- struct{}{} |
| 57 | + }() |
| 58 | + } |
| 59 | + |
| 60 | + // Set up a queue module configuration for an instance. |
| 61 | + q := queue.New(conn, h.name) |
| 62 | + opts := queue.CfgOpts{InReplicaset: true, Ttr: 60 * time.Second} |
| 63 | + |
| 64 | + if h.err = q.Cfg(opts); h.err != nil { |
| 65 | + return fmt.Errorf("unable to configure queue: %w", h.err) |
| 66 | + } |
| 67 | + |
| 68 | + // The queue only works with a master instance. |
| 69 | + if !master { |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + if h.err = q.Create(h.cfg); h.err != nil { |
| 74 | + return h.err |
| 75 | + } |
| 76 | + |
| 77 | + if !h.registered { |
| 78 | + // We register a shared session at the first time. |
| 79 | + if h.uuid, h.err = q.Identify(nil); h.err != nil { |
| 80 | + return h.err |
| 81 | + } |
| 82 | + h.registered = true |
| 83 | + } else { |
| 84 | + // We re-identify as the shared session. |
| 85 | + if _, h.err = q.Identify(&h.uuid); h.err != nil { |
| 86 | + return h.err |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fmt.Printf("Master %s is ready to work!\n", conn.Addr()) |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// Deactivated doesn't do anything useful for the example. |
| 96 | +func (h *QueueConnectionHandler) Deactivated(conn *tarantool.Connection, |
| 97 | + role connection_pool.Role) error { |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// Closes closes a QueueConnectionHandler object. |
| 102 | +func (h *QueueConnectionHandler) Close() { |
| 103 | + close(h.masterUpdated) |
| 104 | +} |
| 105 | + |
| 106 | +// Example demonstrates how to use the queue package with the connection_pool |
| 107 | +// package. First of all, you need to create a ConnectionHandler implementation |
| 108 | +// for the a ConnectionPool object to process new connections from |
| 109 | +// RW-instances. |
| 110 | +// |
| 111 | +// You need to register a shared session UUID at a first master connection. |
| 112 | +// It needs to be used to re-identify as the shared session on new |
| 113 | +// RW-instances. See QueueConnectionHandler.Discovered() implementation. |
| 114 | +// |
| 115 | +// After that, you need to create a ConnectorAdapter object with RW mode for |
| 116 | +// the ConnectionPool to send requests into RW-instances. This adapter can |
| 117 | +// be used to create a ready-to-work queue object. |
| 118 | +func Example_connectionPool() { |
| 119 | + // Create a ConnectionHandler object. |
| 120 | + cfg := queue.Cfg{ |
| 121 | + Temporary: false, |
| 122 | + IfNotExists: true, |
| 123 | + Kind: queue.FIFO, |
| 124 | + Opts: queue.Opts{ |
| 125 | + Ttl: 10 * time.Second, |
| 126 | + }, |
| 127 | + } |
| 128 | + h := NewQueueConnectionHandler("test_queue", cfg) |
| 129 | + defer h.Close() |
| 130 | + |
| 131 | + // Create a ConnectionPool object. |
| 132 | + servers := []string{ |
| 133 | + "127.0.0.1:3014", |
| 134 | + "127.0.0.1:3015", |
| 135 | + } |
| 136 | + connOpts := tarantool.Opts{ |
| 137 | + Timeout: 1 * time.Second, |
| 138 | + User: "test", |
| 139 | + Pass: "test", |
| 140 | + } |
| 141 | + poolOpts := connection_pool.OptsPool{ |
| 142 | + CheckTimeout: 1 * time.Second, |
| 143 | + ConnectionHandler: h, |
| 144 | + } |
| 145 | + connPool, err := connection_pool.ConnectWithOpts(servers, connOpts, poolOpts) |
| 146 | + if err != nil { |
| 147 | + fmt.Printf("Unable to connect to the pool: %s", err) |
| 148 | + return |
| 149 | + } |
| 150 | + defer connPool.Close() |
| 151 | + |
| 152 | + // Wait for a master instance identification in the queue. |
| 153 | + <-h.masterUpdated |
| 154 | + if h.err != nil { |
| 155 | + fmt.Printf("Unable to identify in the pool: %s", h.err) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + // Create a Queue object from the ConnectionPool object via |
| 160 | + // a ConnectorAdapter. |
| 161 | + rw := connection_pool.NewConnectorAdapter(connPool, connection_pool.RW) |
| 162 | + q := queue.New(rw, "test_queue") |
| 163 | + fmt.Println("A Queue object is ready to work.") |
| 164 | + |
| 165 | + testData := "test_data" |
| 166 | + fmt.Println("Send data:", testData) |
| 167 | + if _, err = q.Put(testData); err != nil { |
| 168 | + fmt.Printf("Unable to put data into the queue: %s", err) |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + // Switch a master instance in the pool. |
| 173 | + roles := []bool{true, false} |
| 174 | + err = test_helpers.SetClusterRO(servers, connOpts, roles) |
| 175 | + if err != nil { |
| 176 | + fmt.Printf("Unable to set cluster roles: %s", err) |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + // Wait for a new master instance re-identification. |
| 181 | + <-h.masterUpdated |
| 182 | + if h.err != nil { |
| 183 | + fmt.Printf("Unable to re-identify in the pool: %s", h.err) |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + // Take a data from the new master instance. |
| 188 | + task, err := q.TakeTimeout(1 * time.Second) |
| 189 | + if err != nil { |
| 190 | + fmt.Println("Unable to got task:", err) |
| 191 | + } else if task == nil { |
| 192 | + fmt.Println("task == nil") |
| 193 | + } else if task.Data() == nil { |
| 194 | + fmt.Println("task.Data() == nil") |
| 195 | + } else { |
| 196 | + task.Ack() |
| 197 | + fmt.Println("Got data:", task.Data()) |
| 198 | + } |
| 199 | + |
| 200 | + // Output: |
| 201 | + // Master 127.0.0.1:3014 is ready to work! |
| 202 | + // A Queue object is ready to work. |
| 203 | + // Send data: test_data |
| 204 | + // Master 127.0.0.1:3015 is ready to work! |
| 205 | + // Got data: test_data |
| 206 | +} |
0 commit comments