RAFT Consensus
gradysim.protocol.plugin.raft
RAFT Consensus Plugin for GrADyS-SIM
This module provides distributed consensus capabilities using the RAFT algorithm. It enables protocols to reach agreement on shared values in a fault-tolerant manner.
Key Features:
-
Fault-tolerant consensus with node failure handling
-
Active node discovery for dynamic majority calculations
-
Heartbeat-based failure detection
-
Dual operation modes (Classic and Fault-Tolerant)
-
Seamless integration with GrADyS-SIM protocols
-
Consensus variables instead of traditional log replication
Example:
from gradysim.protocol.plugin.raft import RaftConfig, RaftMode, RaftConsensusPlugin
# Configure consensus
config = RaftConfig()
config.set_election_timeout(150, 300)
config.set_heartbeat_interval(50)
config.add_consensus_variable("sequence", int)
config.set_raft_mode(RaftMode.FAULT_TOLERANT)
# Initialize and start
consensus = RaftConsensusPlugin(config=config, protocol=self)
consensus.set_known_nodes([0, 1, 2, 3, 4])
consensus.start()
# Propose values (leader only)
if consensus.is_leader():
consensus.propose_value("sequence", 42)
FailureConfig
Configuration for heartbeat-based failure detection.
This class manages all parameters related to failure detection, including thresholds, intervals, and timeouts.
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
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 |
|
detection_interval: int
property
Get the detection interval.
failure_threshold: int
property
Get the failure threshold.
heartbeat_timeout_ms: int
property
Get the heartbeat timeout in milliseconds (calculated dynamically).
recovery_threshold: int
property
Get the recovery threshold.
__str__()
String representation of the configuration.
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
get_absolute_timeout_ms()
Get the absolute timeout value.
Returns:
Type | Description |
---|---|
Optional[int]
|
Absolute timeout in milliseconds, or None if not set |
get_heartbeat_interval_reference()
Get the heartbeat interval reference.
Returns:
Type | Description |
---|---|
Optional[int]
|
Heartbeat interval in milliseconds, or None if not set |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
get_timeout_ms()
Get the timeout in milliseconds.
Returns:
Type | Description |
---|---|
int
|
Timeout in milliseconds (absolute or calculated as multiplier × heartbeat_interval) |
Raises: ValueError: If neither absolute timeout nor heartbeat_interval/multiplier is set
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
get_timeout_multiplier()
Get the current timeout as a multiplier of heartbeat interval.
Returns:
Type | Description |
---|---|
Optional[int]
|
Multiplier value, or None if heartbeat_interval is not set |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
is_using_absolute_timeout()
Check if absolute timeout is being used.
Returns:
Type | Description |
---|---|
bool
|
True if absolute timeout is enabled, False otherwise |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
set_absolute_timeout(timeout_ms)
Set an absolute timeout for heartbeat responses independent of heartbeat_interval.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout_ms |
int
|
Timeout in milliseconds (must be positive) |
required |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
set_detection_interval(interval)
Set how often to check for failures (in heartbeat intervals).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interval |
int
|
Check every N heartbeats (default: 2) |
required |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
set_failure_threshold(threshold)
Set the number of consecutive failed heartbeats to mark a node as failed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
threshold |
int
|
Number of failed heartbeats (default: 3) |
required |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
set_heartbeat_interval_reference(interval_ms)
Set the heartbeat interval reference. This is called internally by RaftConfig when heartbeat_interval is set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interval_ms |
int
|
Heartbeat interval in milliseconds |
required |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
set_heartbeat_timeout(multiplier)
Set the timeout for heartbeat responses as a multiple of heartbeat_interval.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
multiplier |
int
|
Number of heartbeat intervals to wait (e.g., 4 = 4× heartbeat_interval) |
required |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
set_recovery_threshold(threshold)
Set the number of consecutive successful heartbeats to mark a node as recovered.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
threshold |
int
|
Number of successful heartbeats (default: 2) |
required |
Source code in gradysim/protocol/plugin/raft/failure_detection/failure_config.py
RaftConfig
Configuration class for Raft consensus using Builder pattern.
Provides a fluent interface for configuring all aspects of Raft consensus:
-
Election timeouts
-
Heartbeat intervals
-
Consensus variables
-
Logging options
Example:
config = RaftConfig()
config.set_election_timeout(150, 300)
config.set_heartbeat_interval(50)
config.add_consensus_variable("sequence", int)
config.set_logging(True, "INFO")
Source code in gradysim/protocol/plugin/raft/raft_config.py
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 |
|
__init__()
Initialize RaftConfig with default values.
Source code in gradysim/protocol/plugin/raft/raft_config.py
__repr__()
Return detailed string representation of the configuration.
Source code in gradysim/protocol/plugin/raft/raft_config.py
__str__()
add_consensus_variable(name, var_type)
Add a consensus variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the consensus variable |
required |
var_type |
Type
|
Type of the variable (e.g., int, str, float) |
required |
Returns:
Type | Description |
---|---|
RaftConfig
|
Self for method chaining |
Raises:
Type | Description |
---|---|
ValueError
|
If variable name is empty or already exists |
Source code in gradysim/protocol/plugin/raft/raft_config.py
get_consensus_variable_type(name)
Get the type of a consensus variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the consensus variable |
required |
Returns:
Type | Description |
---|---|
Optional[Type]
|
Type of the variable, or None if not found |
Source code in gradysim/protocol/plugin/raft/raft_config.py
get_consensus_variables()
Get all configured consensus variables.
Returns:
Type | Description |
---|---|
Dict[str, Type]
|
Dictionary mapping variable names to their types |
get_failure_config()
Get the failure detection configuration.
Returns:
Type | Description |
---|---|
FailureConfig
|
FailureConfig instance for configuring failure detection |
get_raft_mode()
get_random_election_timeout()
Get a random election timeout within the configured range.
Returns:
Type | Description |
---|---|
int
|
Random election timeout in milliseconds |
Source code in gradysim/protocol/plugin/raft/raft_config.py
has_consensus_variable(name)
Check if a consensus variable exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the consensus variable |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the variable exists, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_config.py
is_classic_mode()
Check if running in classic Raft mode.
Returns:
Type | Description |
---|---|
bool
|
True if classic mode, False if fault-tolerant mode |
is_fault_tolerant_mode()
Check if running in fault-tolerant Raft mode.
Returns:
Type | Description |
---|---|
bool
|
True if fault-tolerant mode, False if classic mode |
remove_consensus_variable(name)
Remove a consensus variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the consensus variable to remove |
required |
Returns:
Type | Description |
---|---|
RaftConfig
|
Self for method chaining |
Source code in gradysim/protocol/plugin/raft/raft_config.py
set_election_timeout(min_timeout, max_timeout)
Set election timeout range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
min_timeout |
int
|
Minimum election timeout in milliseconds |
required |
max_timeout |
int
|
Maximum election timeout in milliseconds |
required |
Returns:
Type | Description |
---|---|
RaftConfig
|
Self for method chaining |
Raises:
Type | Description |
---|---|
ValueError
|
If min_timeout >= max_timeout or timeouts are negative |
Source code in gradysim/protocol/plugin/raft/raft_config.py
set_heartbeat_interval(interval)
Set heartbeat interval.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interval |
int
|
Heartbeat interval in milliseconds |
required |
Returns:
Type | Description |
---|---|
RaftConfig
|
Self for method chaining |
Raises:
Type | Description |
---|---|
ValueError
|
If interval is negative |
Source code in gradysim/protocol/plugin/raft/raft_config.py
set_logging(enable, level='INFO')
Configure logging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
enable |
bool
|
Whether to enable logging |
required |
level |
str
|
Log level (DEBUG, INFO, WARNING, ERROR) |
'INFO'
|
Returns:
Type | Description |
---|---|
RaftConfig
|
Self for method chaining |
Source code in gradysim/protocol/plugin/raft/raft_config.py
set_raft_mode(mode)
Set the Raft operation mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode |
RaftMode
|
Raft operation mode CLASSIC - Classic Raft behavior (fixed cluster size) FAULT_TOLERANT - Fault-tolerant Raft behavior (dynamic active node count) |
required |
Returns:
Type | Description |
---|---|
RaftConfig
|
Self for method chaining |
Source code in gradysim/protocol/plugin/raft/raft_config.py
to_dict()
Convert configuration to dictionary.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary representation of the configuration |
Source code in gradysim/protocol/plugin/raft/raft_config.py
validate()
Validate the configuration.
Returns:
Type | Description |
---|---|
List[str]
|
List of validation errors (empty if valid) |
Source code in gradysim/protocol/plugin/raft/raft_config.py
RaftConsensusPlugin
Main interface for Raft consensus implementation.
Provides a simple, clean API for integrating Raft consensus into Gradysim protocols. This class implements the Facade pattern to hide the complexity of the underlying Raft implementation.
Main Methods
- start() / stop(): Control consensus lifecycle
- propose_value(): Propose new values (leader only)
- get_committed_value() / get_all_committed_values(): Retrieve consensus values
- is_leader() / get_leader_id(): Check leadership status
- get_current_term() / get_current_state(): Get consensus state
- handle_message() / handle_timer(): Process Raft protocol events
- set_known_nodes(): Configure cluster membership
- get_statistics() / get_state_info(): Get debugging information
Example
1. Configure consensus
config = RaftConfig() config.set_election_timeout(150, 300) # 150-300ms election timeout config.set_heartbeat_interval(50) # 50ms heartbeat interval config.add_consensus_variable("sequence", int) config.add_consensus_variable("leader_position", str) config.set_logging(enable=True, level="INFO")
2. Create consensus instance (simplified)
consensus = RaftConsensus(config=config, protocol=protocol)
3. Set known nodes and start consensus
consensus.set_known_nodes([1, 2, 3, 4, 5]) consensus.start()
4. Propose values (only works if this node is leader)
if consensus.is_leader(): consensus.propose_value("sequence", 42) consensus.propose_value("leader_position", "north")
5. Get committed values
sequence_value = consensus.get_committed_value("sequence") position_value = consensus.get_committed_value("leader_position")
6. Get all committed values
all_values = consensus.get_all_committed_values()
7. Check consensus state
is_leader = consensus.is_leader() leader_id = consensus.get_leader_id() current_term = consensus.get_current_term() current_state = consensus.get_current_state()
8. Handle messages and timers (call these from your protocol)
consensus.handle_message(message_str) consensus.handle_timer("heartbeat") consensus.handle_timer("election")
9. Get statistics and information
stats = consensus.get_statistics() state_info = consensus.get_state_info() config_info = consensus.get_configuration()
10. Check if system is ready
if consensus.is_ready(): print("Consensus system is ready")
11. Check failure detection (if enabled)
failed_nodes = consensus.get_failed_nodes() active_nodes = consensus.get_active_nodes() if consensus.is_node_failed(3): print("Node 3 is currently failed")
12. Stop consensus when done
consensus.stop()
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
|
__init__(config, protocol)
Initialize Raft consensus plugin with Gradysim protocol provider.
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
configure_handle_message()
Intercept RAFT packets and route them to the internal node.
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
configure_handle_timer()
Intercept RAFT timers created by the plugin.
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_active_nodes()
Get the set of currently active nodes. DEPRECATED: Use get_simulation_active_nodes() or get_communication_active_nodes() instead.
Returns:
Type | Description |
---|---|
Set[int]
|
Set of active node IDs, empty if failure detection is disabled |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_active_nodes_info()
Get detailed information about active nodes from this node's perspective. This method provides comprehensive information about cluster state and active nodes.
Works in both CLASSIC and FAULT_TOLERANT modes with appropriate behavior for each:
CLASSIC mode:
-
All known nodes are considered active (no failure detection)
-
Returns information based on the complete known node list
FAULT_TOLERANT mode:
-
Uses actual failure detection to determine active nodes
-
Information accuracy differs by node role:
-
Leader: Complete and accurate active nodes information from heartbeat detection
-
Follower/Candidate: Active count from leader + limited local node knowledge
Can be called on any node (leader, candidate, or follower) in any mode. Use this method to get detailed monitoring information about which nodes are active, failed, and the current majority status from the perspective of the calling node.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary containing: |
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Example
# Works in both modes
active_info = consensus.get_active_nodes_info()
print(f"Node {active_info['current_node_id']} role: {active_info['node_role']}")
print(f"Active nodes: {active_info['active_nodes']}")
print(f"Failed nodes: {active_info['failed_nodes']}")
print(f"Has majority: {active_info['has_majority']}")
print(f"Detection method: {active_info['detection_method']}")
print(f"Raft mode: {active_info['raft_mode']}")
if active_info['is_leader']:
print("This node is the leader")
elif active_info['leader_id']:
print(f"Leader is node {active_info['leader_id']}")
else:
print("No current leader")
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_all_committed_values()
Get all committed consensus values.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary of all committed values |
get_cluster_id()
Get the cluster ID for this node.
Returns:
Type | Description |
---|---|
Optional[int]
|
Cluster ID, or None if not set |
get_committed_value(variable_name)
Get the committed value for a consensus variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_name |
str
|
Name of the consensus variable |
required |
Returns:
Type | Description |
---|---|
Optional[Any]
|
Committed value, or None if not available |
Raises:
Type | Description |
---|---|
ValueError
|
If variable is not configured |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_communication_active_nodes()
Get the set of nodes that have active communication. This is based on heartbeat detection.
Returns:
Type | Description |
---|---|
Set[int]
|
Set of communication active node IDs |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_communication_failed_nodes()
Get the set of nodes that have communication failures. This is based on heartbeat detection.
Returns:
Type | Description |
---|---|
Set[int]
|
Set of communication failed node IDs |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_configuration()
Get the current configuration.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary representation of the configuration |
get_consensus_variable_type(variable_name)
Get the type of a consensus variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_name |
str
|
Name of the consensus variable |
required |
Returns:
Type | Description |
---|---|
Optional[Type]
|
Type of the variable, or None if not found |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_consensus_variables()
Get all configured consensus variables.
Returns:
Type | Description |
---|---|
Dict[str, Type]
|
Dictionary mapping variable names to their types |
get_current_state()
Get the current state of this node.
Returns:
Type | Description |
---|---|
RaftState
|
Current Raft state (FOLLOWER, CANDIDATE, or LEADER) |
get_current_term()
Get the current term.
Returns:
Type | Description |
---|---|
int
|
Current term number |
get_failed_nodes()
Get the set of currently failed nodes. DEPRECATED: Use get_communication_failed_nodes() instead.
Returns:
Type | Description |
---|---|
Set[int]
|
Set of failed node IDs, empty if failure detection is disabled |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_failure_detection_metrics()
Get detailed metrics about failure detection performance.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary with detailed failure detection metrics, or empty dict if not available |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_is_active(node_id)
Check if a specific node is currently active. DEPRECATED: Use is_simulation_active() or is_communication_failed() instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id |
int
|
ID of the node to check |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the node is active, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_leader_id()
Get the current leader ID.
Returns:
Type | Description |
---|---|
Optional[int]
|
ID of the current leader, or None if no leader is known |
get_majority_info()
Get detailed information about majority status.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary with majority information including active nodes, |
Dict[str, Any]
|
majority threshold, and current status |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_node_id()
Get the current node ID.
If a get_node_id_callback was provided during initialization, this method will call it to get the current node ID dynamically. Otherwise, it returns the static node_id.
Returns:
Type | Description |
---|---|
int
|
Current node ID |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_simulation_active_nodes()
Get the set of nodes that are active in simulation. This is based on manual control (active/inactive state).
Returns:
Type | Description |
---|---|
Set[int]
|
Set of simulation active node IDs |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
get_state_info()
Get current state information for debugging.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary with current state information |
get_statistics()
Get consensus statistics.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary with consensus statistics |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
has_consensus_variable(variable_name)
Check if a consensus variable is configured.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_name |
str
|
Name of the consensus variable |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the variable is configured, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
has_majority_confirmation()
Check if majority of active nodes have confirmed current values.
Returns:
Type | Description |
---|---|
bool
|
True if majority of active nodes have confirmed, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
has_majority_votes()
Check if this node has received majority of votes in current election.
Returns:
Type | Description |
---|---|
bool
|
True if majority of active nodes have voted for this node, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
has_quorum()
Check if the system has enough active nodes to form a quorum.
Returns:
Type | Description |
---|---|
bool
|
True if there are enough active nodes to operate, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
is_communication_failed(node_id)
Check if a specific node has communication failure. This is based on heartbeat detection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id |
int
|
ID of the node to check |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the node has communication failure, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
is_in_same_cluster(other_node_id)
Check if another node is in the same cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other_node_id |
int
|
ID of the other node to check |
required |
Returns:
Type | Description |
---|---|
bool
|
True if both nodes are in the same cluster, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
is_leader()
Check if this node is the current leader.
Returns:
Type | Description |
---|---|
bool
|
True if this node is the leader, False otherwise |
is_node_failed(node_id)
Check if a specific node is currently failed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id |
int
|
ID of the node to check |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the node is failed, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
is_ready()
Check if the consensus system is ready.
Returns:
Type | Description |
---|---|
bool
|
True if the system is ready, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
is_simulation_active(node_id)
Check if a specific node is currently active in simulation. This is the manual control state (active/inactive).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id |
int
|
ID of the node to check |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the node is active in simulation, False otherwise |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
propose_value(variable_name, value)
Propose a new value for consensus.
This method can only be called by the current leader. If this node is not the leader, the proposal will be rejected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_name |
str
|
Name of the consensus variable |
required |
value |
Any
|
Value to propose |
required |
Returns:
Type | Description |
---|---|
bool
|
True if proposal was accepted, False otherwise |
Raises:
Type | Description |
---|---|
ValueError
|
If variable is not configured or value type is invalid |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
send_broadcast(message)
Send broadcast message to all nodes.
This method sends a message to all nodes in the cluster using broadcast communication if available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
str
|
Message content to broadcast |
required |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
set_cluster_id(cluster_id)
Set the cluster ID for this node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cluster_id |
Optional[int]
|
Cluster ID to assign to this node, or None to clear |
required |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
set_is_active(node_id, active)
Set this node's active/inactive state. DEPRECATED: Use set_simulation_active() instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id |
int
|
ID of the node to set state |
required |
active |
bool
|
True to make node active, False to make it inactive |
required |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
set_known_nodes(node_ids)
Set the list of known node IDs.
This method should be called to inform the consensus system about all nodes in the cluster. This information is used for sending messages during elections and heartbeats.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_ids |
List[int]
|
List of all node IDs in the cluster |
required |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
set_simulation_active(node_id, active)
Set this node's simulation active/inactive state. Only affects this node if node_id matches this node's ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id |
int
|
ID of the node to set state |
required |
active |
bool
|
True to make node active in simulation, False to make it inactive |
required |
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
start()
Start the consensus process.
This method initializes the Raft node and begins the election timeout. The node will start as a follower and may become a candidate if no leader is discovered.
Source code in gradysim/protocol/plugin/raft/raft_consensus.py
stop()
Stop the consensus process.
This method stops the Raft node and cancels all active timers.
RaftMode
Bases: Enum
Enumeration for Raft operation modes.
CLASSIC: Classic Raft mode
- uses fixed cluster size for all calculations
FAULT_TOLERANT: Fault-tolerant Raft mode
- uses active node count for majority calculations
Source code in gradysim/protocol/plugin/raft/raft_config.py
RaftState
Bases: Enum
Raft node states enumeration.
Each state represents a different role and behavior in the Raft consensus algorithm.
Source code in gradysim/protocol/plugin/raft/raft_state.py
CANDIDATE = auto()
class-attribute
instance-attribute
Candidate state - Used during leader election.
Responsibilities:
-
Increment current term
-
Vote for self
-
Request votes from other nodes
-
Become leader if majority votes received
-
Return to follower if another leader discovered
FOLLOWER = auto()
class-attribute
instance-attribute
Follower state - Default state for all nodes.
Responsibilities:
-
Respond to requests from leaders and candidates
-
Start election if election timeout elapses
-
Vote for candidates during elections
-
Accept log entries from leader
LEADER = auto()
class-attribute
instance-attribute
Leader state - Handles all client requests and log replication.
Responsibilities:
-
Send periodic heartbeats to all followers
-
Handle client requests
-
Replicate log entries to followers
-
Commit entries when majority replicated
-
Step down if higher term discovered