|
7 | 7 | "time" |
8 | 8 |
|
9 | 9 | "github.com/unpackdev/fdb/pkg/logger" |
| 10 | + "github.com/unpackdev/fdb/pkg/messages" |
| 11 | + "github.com/unpackdev/fdb/pkg/types" |
10 | 12 | "github.com/unpackdev/fdb/playground/suite" |
11 | 13 | ) |
12 | 14 |
|
@@ -109,12 +111,14 @@ func WithTotalOperations(ops int) WriteStrategyOption { |
109 | 111 |
|
110 | 112 | // Start begins executing the write strategy |
111 | 113 | func (s *WriteStrategy) Start(ctx context.Context) error { |
112 | | - s.logger.Info("Starting write strategy", |
| 114 | + s.logger.Info( |
| 115 | + "Starting write strategy", |
113 | 116 | "workers", s.workersCount, |
114 | 117 | "data_size_kb", s.dataSizeKB, |
115 | 118 | "ops_per_sec", s.opsPerSec, |
116 | 119 | "total_ops", s.totalOps, |
117 | | - "target_node", s.targetNode.PeerID().String()) |
| 120 | + "target_node", s.targetNode.PeerID().String(), |
| 121 | + ) |
118 | 122 |
|
119 | 123 | ctx, s.cancel = context.WithCancel(ctx) |
120 | 124 | s.startTime = time.Now() |
@@ -209,20 +213,53 @@ func (s *WriteStrategy) runWorker(ctx context.Context, id, ops int, delay time.D |
209 | 213 |
|
210 | 214 | // performWriteOperation executes a single write operation |
211 | 215 | func (s *WriteStrategy) performWriteOperation(ctx context.Context, workerID, opID int) error { |
212 | | - key := fmt.Sprintf("%s%d-%d", s.keyPrefix, workerID, opID) |
| 216 | + //key := fmt.Sprintf("%s%d-%d", s.keyPrefix, workerID, opID) |
213 | 217 |
|
214 | 218 | data, err := suite.GenerateTestDataKB(s.dataSizeKB, false) |
215 | 219 | if err != nil { |
216 | 220 | return fmt.Errorf("failed to generate test data: %w", err) |
217 | 221 | } |
218 | 222 |
|
219 | | - // TODO: Implement actual write operation using the client |
220 | | - // This is a placeholder for the actual implementation that would use the |
221 | | - // optimized BatchWriter component with its 2048 batch size and 100ms flush interval |
| 223 | + key, err := messages.GenerateRandomKey() |
| 224 | + if err != nil { |
| 225 | + return fmt.Errorf("failed to generate random key: %w", err) |
| 226 | + } |
222 | 227 |
|
223 | | - s.logger.Debug("Write operation", |
| 228 | + // Create a write message with the key and data |
| 229 | + writeMsg := &messages.Message{ |
| 230 | + Handler: types.WriteHandlerType, |
| 231 | + Key: key, |
| 232 | + Data: data, |
| 233 | + } |
| 234 | + |
| 235 | + // Encode the message |
| 236 | + encodedWriteMsg, err := writeMsg.Encode() |
| 237 | + if err != nil { |
| 238 | + return fmt.Errorf("failed to encode write message: %w", err) |
| 239 | + } |
| 240 | + |
| 241 | + // Send the message to the target node using the client |
| 242 | + // This leverages the optimized BatchWriter component with 2048 batch size and 100ms flush interval |
| 243 | + // that's already configured in the underlying implementation |
| 244 | + response, err := s.targetNode.SendAndReceiveMessage( |
| 245 | + ctx, |
| 246 | + s.targetNode, |
| 247 | + types.TCPTransportType, |
| 248 | + types.WriteHandlerType, |
| 249 | + encodedWriteMsg, |
| 250 | + 5*time.Second, |
| 251 | + ) |
| 252 | + if err != nil { |
| 253 | + return fmt.Errorf("failed to write record to target node: %w", err) |
| 254 | + } |
| 255 | + |
| 256 | + s.logger.Debug( |
| 257 | + "Write operation successful", |
224 | 258 | "key", key, |
225 | | - "data_size", len(data)) |
| 259 | + "data_size", len(data), |
| 260 | + "worker_id", workerID, |
| 261 | + "response", response, |
| 262 | + ) |
226 | 263 |
|
227 | 264 | return nil |
228 | 265 | } |
|
0 commit comments