-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraid-controller.c
More file actions
106 lines (97 loc) · 3.31 KB
/
raid-controller.c
File metadata and controls
106 lines (97 loc) · 3.31 KB
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
/**
* RAID File Server Farm Simulation
*
* FILE: raid-controller.c
* AUTHOR: Matt Perry
* DATE: Apr 27 2011
*/
#include "raid.h"
// Helpers
void raid_controller_gen_send( tw_lpid dest, Event event_type, tw_stime event_time, tw_lp* lp )
{
// Generate and send message to dest
tw_event* event = tw_event_new( dest, event_time, lp );
MsgData* message = (MsgData*)tw_event_data( event );
message->event_type = event_type;
tw_event_send( event );
}
tw_lpid raid_controller_find_server( tw_lp* lp )
{
return ( LPS_PER_FS * ( lp->gid / LPS_PER_FS ) ) + FS_PER_IO;
}
// Initialize
void raid_controller_init( ContState* s, tw_lp* lp )
{
// Initialize state
s->m_server = raid_controller_find_server( lp );
s->condition = HEALTHY;
s->num_rebuilds = 0;
}
// Event Handler
void raid_controller_eventhandler( ContState* s, tw_bf* cv, MsgData* m, tw_lp* lp )
{
// Save the controller's initial condition
m->rc.controller_condition = s->condition;
// Only handle events if the controller isnt dead
if( s->condition != FAILED )
{
// Check message type
switch( m->event_type )
{
case DISK_FAILURE:
switch( s->condition )
{
case WAIT:
s->condition = FAILED;
break;
case REBUILD:
s->condition = FAILED;
// Generate and send message to the file server
raid_controller_gen_send( s->m_server, RAID_FAILURE, 0, lp );
break;
default:
s->condition = WAIT;
// Generate and send message to the file server
raid_controller_gen_send( s->m_server, DISK_FAILURE, 0, lp );
break;
}
break;
case DISK_REPLACED:
s->condition = REBUILD;
s->num_rebuilds++;
// Schedule rebuild completion
tw_stime rebuild_time = tw_rand_normal_sd( lp->rng, REBUILD_TIME,
STD_DEV * REBUILD_TIME, &(m->rc.rng_calls) );
raid_controller_gen_send( lp->gid, REBUILD_FINISH, rebuild_time, lp );
// Generate and send message to the file server
raid_controller_gen_send( s->m_server, REBUILD_START, 0, lp );
break;
case REBUILD_FINISH:
s->condition = HEALTHY;
// Generate and send message to the file server
raid_controller_gen_send( s->m_server, REBUILD_FINISH, 0, lp );
break;
default:
message_error( m->event_type );
}
}
}
// Reverse Event Handler
void raid_controller_eventhandler_rc( ContState* s, tw_bf* cv, MsgData* m, tw_lp* lp )
{
int i;
s->condition = m->rc.controller_condition;
if( m->event_type == DISK_REPLACED || s->condition != FAILED )
{
s->num_rebuilds--;
for( i = 0; i < m->rc.rng_calls; ++i )
tw_rand_reverse_unif( lp->rng );
}
}
// Finish
void raid_controller_finish( ContState* s, tw_lp* lp )
{
g_stats.ttl_controller_rebuilds += s->num_rebuilds;
if( s->condition == FAILED )
g_stats.ttl_controller_failures++;
}