File: stream-notifications.inc

package info (click to toggle)
php-mongo 1.5.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,040 kB
  • ctags: 2,802
  • sloc: ansic: 17,632; xml: 2,195; php: 1,630; pascal: 330; makefile: 52; sh: 39
file content (108 lines) | stat: -rw-r--r-- 3,191 bytes parent folder | download
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
<?php

class MongoNotifications {
    public $reqs = array();
    public $inserts = array();


    function update($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
        switch($notification_code) {
            case MONGO_STREAM_NOTIFY_TYPE_LOG:
                return $this->logMessage($message_code, $message);
            default:
        }
    }

    function logMessage($code, $message) {
        $data = unserialize($message);

        switch($code) {
            case MONGO_STREAM_NOTIFY_LOG_CMD_INSERT:
            case MONGO_STREAM_NOTIFY_LOG_CMD_UPDATE:
            case MONGO_STREAM_NOTIFY_LOG_CMD_DELETE:
                $reqid = $data["protocol_options"]["request_id"];
                $this->reqs[$reqid] = $data;
                $this->reqs[$reqid]["start"] = microtime(true);
                $this->inserts[] = $reqid;
            break;

            case MONGO_STREAM_NOTIFY_LOG_WRITE_REPLY:
                $reqid = $data["op"]["response_to"];

                $end = microtime(true);
                $total = $end - $this->reqs[$reqid]["start"];
                $data["total"] = $total;

                $this->reqs[$reqid]["response"] = $data;
                break;
        }
    }

    function getLastInsertMeta() {
        $ids = $this->getInsertRequestIDs();
        $id = array_pop($ids);
        return $this->getRequest($id);
    }
    function getInsertRequestIDs() {
        return $this->inserts;
    }
    function getRequest($n) {
        return $this->reqs[$n];
    }
    function getRequests() {
        return $this->reqs;
    }
}

/* Some Helper Functions */

/* Split a Server Hash to extract the server name from it */
function getServerName($hash) {
    list($server, $replicaset, $dbname, $pid) = explode(";", $hash);

    return $server;
}

/* Resolve the server type to a readable string */
function getServerType($type) {
    switch($type) {
        // FIXME: Do we not export these as constants?
        case 0x01: return "STANDALONE";
        case 0x02: return "PRIMARY";
        case 0x04: return "SECONDARY";
        case 0x10: return "MONGOS";
    }
}

/* Resolve Cursor (Query) Options to something meaningful */
function getCursorOptions($opts) {
    // FIXME: Do we not export these as constants?
    if (($opts & 0x04) == 0x04) {
        return "slaveOkay";
    }
    return "Primary Only";
}



function dump_writes($mn) {
    // Fetch our requests from our logger
    $reqs = $mn->getRequests();
    foreach($reqs as $request) {
        if ($request["response"]["op"]["response_to"] != $request["protocol_options"]["request_id"]) {
            throw Exception("Oboy, this shouldn't happen!");
        }
        /*
        printf("Server name: %s, type: %s, repsponse flags: %d\n",
            getServerName($request["server"]["hash"]),
            getServerType($request["server"]["type"]),
            $request["response"]["reply"]["flags"]
        );
        printf("Server execution time+network latency: %.8f sec\n\n", $request["response"]["total"]);
         */
        var_dump($request["write_options"], $request["protocol_options"]);
        echo "\n\n\n";
    }
}

// vim: set ft=php