File: state.cpp

package info (click to toggle)
watchman 4.9.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,992 kB
  • sloc: cpp: 27,459; python: 6,538; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (254 lines) | stat: -rw-r--r-- 7,549 bytes parent folder | download | duplicates (3)
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
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
/* Copyright 2015-present Facebook, Inc.
 * Licensed under the Apache License, Version 2.0 */

#include "watchman.h"
#include "make_unique.h"

using ms = std::chrono::milliseconds;

struct state_arg {
  w_string name;
  ms sync_timeout;
  json_ref metadata;
};

// Parses the args for state-enter and state-leave
static bool parse_state_arg(
    struct watchman_client* client,
    const json_ref& args,
    struct state_arg* parsed) {
  parsed->sync_timeout = DEFAULT_QUERY_SYNC_MS;
  parsed->metadata = nullptr;
  parsed->name = nullptr;

  if (json_array_size(args) != 3) {
    send_error_response(
        client,
        "invalid number of arguments, expected 3, got %" PRIsize_t,
        json_array_size(args));
    return false;
  }

  const auto& state_args = args.at(2);

  // [cmd, root, statename]
  if (json_is_string(state_args)) {
    parsed->name = json_to_w_string(state_args);
    return true;
  }

  // [cmd, root, {name:, metadata:, sync_timeout:}]
  parsed->name = json_to_w_string(state_args.get("name"));
  parsed->metadata = state_args.get_default("metadata");
  parsed->sync_timeout = ms(json_integer_value(state_args.get_default(
      "sync_timeout", json_integer(parsed->sync_timeout.count()))));

  if (parsed->sync_timeout < ms::zero()) {
    send_error_response(client, "sync_timeout must be >= 0");
    return false;
  }

  return true;
}

watchman_client_state_assertion::watchman_client_state_assertion(
    const std::shared_ptr<w_root_t>& root,
    const w_string& name)
    : root(root), name(name), id(0) {}

static void cmd_state_enter(
    struct watchman_client* clientbase,
    const json_ref& args) {
  struct state_arg parsed;
  json_ref response;
  auto client = dynamic_cast<watchman_user_client*>(clientbase);

  auto root = resolve_root_or_err(client, args, 1, true);
  if (!root) {
    return;
  }

  if (!parse_state_arg(client, args, &parsed)) {
    return;
  }

  if (parsed.sync_timeout.count() && !root->syncToNow(parsed.sync_timeout)) {
    send_error_response(client, "synchronization failed: %s", strerror(errno));
    return;
  }

  auto assertion =
      std::make_shared<watchman_client_state_assertion>(root, parsed.name);
  if (!assertion) {
    send_error_response(client, "out of memory");
    return;
  }

  {
    auto wlock = root->asserted_states.wlock();
    auto& map = *wlock;
    auto& entry = map[assertion->name];

    // If the state is already asserted, we can't re-assert it
    if (entry) {
      send_error_response(
          client, "state %s is already asserted", parsed.name.c_str());
      return;
    }
    // We're now in this state
    entry = assertion;

    // Record the state assertion in the client
    entry->id = ++client->next_state_id;
    client->states[entry->id] = entry;
  }

  // We successfully entered the state, this is our response to the
  // state-enter command.  We do this before we send the subscription
  // PDUs in case CLIENT has active subscriptions for this root
  response = make_response();

  auto clock = w_string_to_json(root->view()->getCurrentClockString());

  response.set({{"root", w_string_to_json(root->root_path)},
                {"state-enter", w_string_to_json(parsed.name)},
                {"clock", json_ref(clock)}});
  send_and_dispose_response(client, std::move(response));

  // Broadcast about the state enter
  {
    auto payload =
        json_object({{"root", w_string_to_json(root->root_path)},
                     {"clock", std::move(clock)},
                     {"state-enter", w_string_to_json(parsed.name)}});
    if (parsed.metadata) {
      payload.set("metadata", json_ref(parsed.metadata));
    }
    root->unilateralResponses->enqueue(std::move(payload));
  }
}
W_CMD_REG("state-enter", cmd_state_enter, CMD_DAEMON, w_cmd_realpath_root)

static void leave_state(
    struct watchman_user_client* client,
    std::shared_ptr<watchman_client_state_assertion> assertion,
    bool abandoned,
    json_t* metadata) {
  // Broadcast about the state leave
  auto payload = json_object(
      {{"root", w_string_to_json(assertion->root->root_path)},
       {"clock",
        w_string_to_json(assertion->root->view()->getCurrentClockString())},
       {"state-leave", w_string_to_json(assertion->name)}});
  if (metadata) {
    payload.set("metadata", json_ref(metadata));
  }
  if (abandoned) {
    payload.set("abandoned", json_true());
  }
  assertion->root->unilateralResponses->enqueue(std::move(payload));

  // The erase will delete the assertion pointer, so save these things
  auto id = assertion->id;
  w_string name = assertion->name;

  // Now remove the state
  {
    auto map = assertion->root->asserted_states.wlock();
    map->erase(name);
  }

  if (client) {
    client->states.erase(id);
  }
}

// Abandon any states that haven't been explicitly vacated
void w_client_vacate_states(struct watchman_user_client *client) {
  while (!client->states.empty()) {
    auto assertion = client->states.begin()->second.lock();

    if (!assertion) {
      client->states.erase(client->states.begin()->first);
      continue;
    }

    auto root = assertion->root;

    w_log(
        W_LOG_ERR,
        "implicitly vacating state %s on %s due to client disconnect\n",
        assertion->name.c_str(),
        root->root_path.c_str());

    // This will delete the state from client->states and invalidate
    // the iterator.
    leave_state(client, assertion, true, nullptr);
  }
}

static void cmd_state_leave(
    struct watchman_client* clientbase,
    const json_ref& args) {
  struct state_arg parsed;
  // This is a weak reference to the assertion.  This is safe because only this
  // client can delete this assertion, and this function is only executed by
  // the thread that owns this client.
  std::shared_ptr<watchman_client_state_assertion> assertion;
  auto client = dynamic_cast<watchman_user_client*>(clientbase);
  json_ref response;

  auto root = resolve_root_or_err(client, args, 1, true);
  if (!root) {
    return;
  }

  if (!parse_state_arg(client, args, &parsed)) {
    return;
  }

  if (parsed.sync_timeout.count() && !root->syncToNow(parsed.sync_timeout)) {
    send_error_response(client, "synchronization failed: %s", strerror(errno));
    return;
  }

  {
    auto map = root->asserted_states.rlock();
    // Confirm that this client owns this state
    const auto& it = map->find(parsed.name);
    // If the state is not asserted, we can't leave it
    if (it == map->end()) {
      send_error_response(
          client, "state %s is not asserted", parsed.name.c_str());
      return;
    }

    assertion = it->second;

    // Sanity check ownership
    if (client->states[assertion->id].lock() != assertion) {
      send_error_response(
          client,
          "state %s was not asserted by this session",
          parsed.name.c_str());
      return;
    }
  }

  // We're about to successfully leave the state, this is our response to the
  // state-leave command.  We do this before we send the subscription
  // PDUs in case CLIENT has active subscriptions for this root
  response = make_response();
  response.set(
      {{"root", w_string_to_json(root->root_path)},
       {"state-leave", w_string_to_json(parsed.name)},
       {"clock", w_string_to_json(root->view()->getCurrentClockString())}});
  send_and_dispose_response(client, std::move(response));

  // Notify and exit the state
  leave_state(client, assertion, false, parsed.metadata);
}
W_CMD_REG("state-leave", cmd_state_leave, CMD_DAEMON, w_cmd_realpath_root)

/* vim:ts=2:sw=2:et:
 */