<commit_before>/* This file is part of VoltDB.
 * Copyright (C) 2008-2012 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.voltdb.dtxn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.zookeeper_voltpatches.WatchedEvent;
import org.apache.zookeeper_voltpatches.Watcher;
import org.apache.zookeeper_voltpatches.ZooKeeper;
import org.json_voltpatches.JSONException;
import org.json_voltpatches.JSONObject;
import org.voltcore.logging.VoltLogger;
import org.voltcore.utils.MiscUtils;

public class MailboxTracker {
    private static final VoltLogger log = new VoltLogger("HOST");

    private final ZooKeeper m_zk;

    private volatile Map<Integer, ArrayList<Long>> m_hostsToSites =
            new HashMap<Integer, ArrayList<Long>>();
    private volatile Map<Integer, ArrayList<Long>> m_partitionsToSites =
            new HashMap<Integer, ArrayList<Long>>();
    private volatile Map<Long, Integer> m_sitesToPartitions =
            new HashMap<Long, Integer>();
    private volatile Map<Integer, Long> m_hostsToPlanners =
            new HashMap<Integer, Long>();
    private volatile Map<Integer, ArrayList<Long>> m_hostsToInitiators =
            new HashMap<Integer, ArrayList<Long>>();

    public MailboxTracker(ZooKeeper zk) throws Exception {
        m_zk = zk;

        getAndWatchSites();
        getAndWatchPlanners();
        getAndWatchInitiators();
    }

    private void getAndWatchSites() throws Exception {
        List<String> children = m_zk.getChildren("/mailboxes/executionsites", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    getAndWatchSites();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        });

        Map<Integer, ArrayList<Long>> hostsToSites = new HashMap<Integer, ArrayList<Long>>();
        Map<Integer, ArrayList<Long>> partitionsToSites = new HashMap<Integer, ArrayList<Long>>();
        Map<Long, Integer> sitesToPartitions = new HashMap<Long, Integer>();
        for (String child : children) {
            byte[] data = m_zk.getData("/mailboxes/executionsites/" + child, false, null);
            JSONObject jsObj = new JSONObject(new String(data, "UTF-8"));
            try {
                long HSId = jsObj.getLong("HSId");
                int partitionId = jsObj.getInt("partitionId");
                int hostId = MiscUtils.getHostIdFromHSId(HSId);

                ArrayList<Long> sites = hostsToSites.get(hostId);
                if (sites == null)
                {
                    sites = new ArrayList<Long>();
                    hostsToSites.put(hostId, sites);
                }
                sites.add(HSId);

                sites = partitionsToSites.get(partitionId);
                if (sites == null) {
                    sites = new ArrayList<Long>();
                    partitionsToSites.put(partitionId, sites);
                }
                sites.add(HSId);

                sitesToPartitions.put(HSId, partitionId);
            } catch (JSONException e) {
                log.error(e.getMessage());
            }
        }

        m_hostsToSites = hostsToSites;
        m_partitionsToSites = partitionsToSites;
        m_sitesToPartitions = sitesToPartitions;
    }

    private void getAndWatchPlanners() throws Exception {
        List<String> children = m_zk.getChildren("/mailboxes/asyncplanners", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    getAndWatchPlanners();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        });

        Map<Integer, Long> hostsToPlanners = new HashMap<Integer, Long>();
        for (String child : children) {
            byte[] data = m_zk.getData("/mailboxes/asyncplanners/" + child, false, null);
            JSONObject jsObj = new JSONObject(new String(data, "UTF-8"));
            try {
                long HSId = jsObj.getLong("HSId");
                hostsToPlanners.put(MiscUtils.getHostIdFromHSId(HSId), HSId);
            } catch (JSONException e) {
                log.error(e.getMessage());
            }
        }

        m_hostsToPlanners = hostsToPlanners;
    }

    private void getAndWatchInitiators() throws Exception {
        List<String> children = m_zk.getChildren("/mailboxes/initiators", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    getAndWatchInitiators();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        });

        Map<Integer, ArrayList<Long>> hostsToInitiators = new HashMap<Integer, ArrayList<Long>>();
        for (String child : children) {
            byte[] data = m_zk.getData("/mailboxes/initiators/" + child, false, null);
            JSONObject jsObj = new JSONObject(new String(data, "UTF-8"));
            try {
                long HSId = jsObj.getLong("HSId");
                int hostId = MiscUtils.getHostIdFromHSId(HSId);

                ArrayList<Long> initiators = new ArrayList<Long>();
                hostsToInitiators.put(hostId, initiators);
                initiators.add(HSId);

                initiators = new ArrayList<Long>();
                initiators.add(HSId);

                // TODO: needs to determine if it's the master or replica
            } catch (JSONException e) {
                log.error(e.getMessage());
            }<commit_msg>Fix mailbox tracker.<commit_after>/* This file is part of VoltDB.
 * Copyright (C) 2008-2012 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.voltdb.dtxn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.zookeeper_voltpatches.WatchedEvent;
import org.apache.zookeeper_voltpatches.Watcher;
import org.apache.zookeeper_voltpatches.ZooKeeper;
import org.json_voltpatches.JSONException;
import org.json_voltpatches.JSONObject;
import org.voltcore.logging.VoltLogger;
import org.voltcore.utils.MiscUtils;

public class MailboxTracker {
    private static final VoltLogger log = new VoltLogger("HOST");

    private final ZooKeeper m_zk;

    private volatile Map<Integer, ArrayList<Long>> m_hostsToSites =
            new HashMap<Integer, ArrayList<Long>>();
    private volatile Map<Integer, ArrayList<Long>> m_partitionsToSites =
            new HashMap<Integer, ArrayList<Long>>();
    private volatile Map<Long, Integer> m_sitesToPartitions =
            new HashMap<Long, Integer>();
    private volatile Map<Integer, Long> m_hostsToPlanners =
            new HashMap<Integer, Long>();
    private volatile Map<Integer, ArrayList<Long>> m_hostsToInitiators =
            new HashMap<Integer, ArrayList<Long>>();

    public MailboxTracker(ZooKeeper zk) throws Exception {
        m_zk = zk;

        getAndWatchSites();
        getAndWatchPlanners();
        getAndWatchInitiators();
    }

    private void getAndWatchSites() throws Exception {
        List<String> children = m_zk.getChildren("/mailboxes/executionsites", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    getAndWatchSites();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        });

        Map<Integer, ArrayList<Long>> hostsToSites = new HashMap<Integer, ArrayList<Long>>();
        Map<Integer, ArrayList<Long>> partitionsToSites = new HashMap<Integer, ArrayList<Long>>();
        Map<Long, Integer> sitesToPartitions = new HashMap<Long, Integer>();
        for (String child : children) {
            byte[] data = m_zk.getData("/mailboxes/executionsites/" + child, false, null);
            JSONObject jsObj = new JSONObject(new String(data, "UTF-8"));
            try {
                long HSId = jsObj.getLong("HSId");
                int partitionId = jsObj.getInt("partitionId");
                int hostId = MiscUtils.getHostIdFromHSId(HSId);

                ArrayList<Long> sites = hostsToSites.get(hostId);
                if (sites == null)
                {
                    sites = new ArrayList<Long>();
                    hostsToSites.put(hostId, sites);
                }
                sites.add(HSId);

                sites = partitionsToSites.get(partitionId);
                if (sites == null) {
                    sites = new ArrayList<Long>();
                    partitionsToSites.put(partitionId, sites);
                }
                sites.add(HSId);

                sitesToPartitions.put(HSId, partitionId);
            } catch (JSONException e) {
                log.error(e.getMessage());
            }
        }

        m_hostsToSites = hostsToSites;
        m_partitionsToSites = partitionsToSites;
        m_sitesToPartitions = sitesToPartitions;
    }

    private void getAndWatchPlanners() throws Exception {
        List<String> children = m_zk.getChildren("/mailboxes/asyncplanners", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    getAndWatchPlanners();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        });

        Map<Integer, Long> hostsToPlanners = new HashMap<Integer, Long>();
        for (String child : children) {
            byte[] data = m_zk.getData("/mailboxes/asyncplanners/" + child, false, null);
            JSONObject jsObj = new JSONObject(new String(data, "UTF-8"));
            try {
                long HSId = jsObj.getLong("HSId");
                hostsToPlanners.put(MiscUtils.getHostIdFromHSId(HSId), HSId);
            } catch (JSONException e) {
                log.error(e.getMessage());
            }
        }

        m_hostsToPlanners = hostsToPlanners;
    }

    private void getAndWatchInitiators() throws Exception {
        List<String> children = m_zk.getChildren("/mailboxes/initiators", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    getAndWatchInitiators();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        });

        Map<Integer, ArrayList<Long>> hostsToInitiators = new HashMap<Integer, ArrayList<Long>>();
        for (String child : children) {
            byte[] data = m_zk.getData("/mailboxes/initiators/" + child, false, null);
            JSONObject jsObj = new JSONObject(new String(data, "UTF-8"));
            try {
                long HSId = jsObj.getLong("HSId");
                int hostId = MiscUtils.getHostIdFromHSId(HSId);

                ArrayList<Long> initiators = hostsToInitiators.get(hostId);
                if (initiators == null) {
                    initiators = new ArrayList<Long>();
                    hostsToInitiators.put(hostId, initiators);
                }
                initiators.add(HSId);
                // TODO: needs to determine if it's the master or replica
            } catch (JSONException e) {
                log.error(e.getMessage());
            }
{'fraction_non_alphanumeric': 0.10017972962413066, 'fraction_numerical': 0.0018754395561459717, 'mean_word_length': 2.1017935046049443, 'pattern_counts': {'":': 0, '<': 86, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6639868', 'n_tokens_mistral': 3538, 'n_tokens_neox': 3375, 'n_words': 867}