From 944e1d96986114df6a38fabd08aae021b8c055ec Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Mon, 1 Apr 2019 18:25:49 -0700 Subject: [PATCH] Remove unused directory command --- .../textsecuregcm/WhisperServerService.java | 1 - .../workers/DirectoryCommand.java | 88 ---------- .../workers/DirectoryUpdater.java | 153 ------------------ 3 files changed, 242 deletions(-) delete mode 100644 src/main/java/org/whispersystems/textsecuregcm/workers/DirectoryCommand.java delete mode 100644 src/main/java/org/whispersystems/textsecuregcm/workers/DirectoryUpdater.java diff --git a/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index e4ee28319..ef0e19a18 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -107,7 +107,6 @@ public class WhisperServerService extends Application bootstrap) { - bootstrap.addCommand(new DirectoryCommand()); bootstrap.addCommand(new VacuumCommand()); bootstrap.addCommand(new TrimMessagesCommand()); bootstrap.addCommand(new DeleteUserCommand()); diff --git a/src/main/java/org/whispersystems/textsecuregcm/workers/DirectoryCommand.java b/src/main/java/org/whispersystems/textsecuregcm/workers/DirectoryCommand.java deleted file mode 100644 index 8e66674ae..000000000 --- a/src/main/java/org/whispersystems/textsecuregcm/workers/DirectoryCommand.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Copyright (C) 2013 Open WhisperSystems - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.whispersystems.textsecuregcm.workers; - -import com.fasterxml.jackson.databind.DeserializationFeature; -import net.sourceforge.argparse4j.inf.Namespace; -import org.skife.jdbi.v2.DBI; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.whispersystems.textsecuregcm.WhisperServerConfiguration; -import org.whispersystems.textsecuregcm.providers.RedisClientFactory; -import org.whispersystems.textsecuregcm.redis.ReplicatedJedisPool; -import org.whispersystems.textsecuregcm.storage.Accounts; -import org.whispersystems.textsecuregcm.storage.AccountsManager; -import org.whispersystems.textsecuregcm.storage.DirectoryManager; - -import io.dropwizard.Application; -import io.dropwizard.cli.EnvironmentCommand; -import io.dropwizard.db.DataSourceFactory; -import io.dropwizard.jdbi.ImmutableListContainerFactory; -import io.dropwizard.jdbi.ImmutableSetContainerFactory; -import io.dropwizard.jdbi.OptionalContainerFactory; -import io.dropwizard.jdbi.args.OptionalArgumentFactory; -import io.dropwizard.setup.Environment; - -public class DirectoryCommand extends EnvironmentCommand { - - private final Logger logger = LoggerFactory.getLogger(DirectoryCommand.class); - - public DirectoryCommand() { - super(new Application() { - @Override - public void run(WhisperServerConfiguration configuration, Environment environment) - throws Exception - { - - } - }, "directory", "Update directory from DB and peers."); - } - - @Override - protected void run(Environment environment, Namespace namespace, - WhisperServerConfiguration configuration) - throws Exception - { - try { - environment.getObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - DataSourceFactory dbConfig = configuration.getReadDataSourceFactory(); - DBI dbi = new DBI(dbConfig.getUrl(), dbConfig.getUser(), dbConfig.getPassword()); - - dbi.registerArgumentFactory(new OptionalArgumentFactory(dbConfig.getDriverClass())); - dbi.registerContainerFactory(new ImmutableListContainerFactory()); - dbi.registerContainerFactory(new ImmutableSetContainerFactory()); - dbi.registerContainerFactory(new OptionalContainerFactory()); - - Accounts accounts = dbi.onDemand(Accounts.class); - ReplicatedJedisPool cacheClient = new RedisClientFactory("main_cache_directory_command", configuration.getCacheConfiguration().getUrl(), configuration.getCacheConfiguration().getReplicaUrls(), configuration.getCacheConfiguration().getCircuitBreakerConfiguration()).getRedisClientPool(); - ReplicatedJedisPool redisClient = new RedisClientFactory("directory_cache_directory_command", configuration.getDirectoryConfiguration().getRedisConfiguration().getUrl(), configuration.getDirectoryConfiguration().getRedisConfiguration().getReplicaUrls(), configuration.getDirectoryConfiguration().getRedisConfiguration().getCircuitBreakerConfiguration()).getRedisClientPool(); - DirectoryManager directory = new DirectoryManager(redisClient); - AccountsManager accountsManager = new AccountsManager(accounts, directory, cacheClient); - - DirectoryUpdater update = new DirectoryUpdater(accountsManager, directory); - - update.updateFromLocalDatabase(); - } catch (Exception ex) { - logger.warn("Directory Exception", ex); - throw new RuntimeException(ex); - } finally { -// Thread.sleep(3000); -// System.exit(0); - } - } -} diff --git a/src/main/java/org/whispersystems/textsecuregcm/workers/DirectoryUpdater.java b/src/main/java/org/whispersystems/textsecuregcm/workers/DirectoryUpdater.java deleted file mode 100644 index 018d1ec64..000000000 --- a/src/main/java/org/whispersystems/textsecuregcm/workers/DirectoryUpdater.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright (C) 2013 Open WhisperSystems - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.whispersystems.textsecuregcm.workers; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.whispersystems.textsecuregcm.entities.ClientContact; -import org.whispersystems.textsecuregcm.storage.Account; -import org.whispersystems.textsecuregcm.storage.AccountsManager; -import org.whispersystems.textsecuregcm.storage.DirectoryManager; -import org.whispersystems.textsecuregcm.storage.DirectoryManager.BatchOperationHandle; -import org.whispersystems.textsecuregcm.util.Util; - -import java.util.List; - -public class DirectoryUpdater { - - private static final int CHUNK_SIZE = 10000; - - private final Logger logger = LoggerFactory.getLogger(DirectoryUpdater.class); - - private final AccountsManager accountsManager; - private final DirectoryManager directory; - - public DirectoryUpdater(AccountsManager accountsManager, DirectoryManager directory) - { - this.accountsManager = accountsManager; - this.directory = directory; - } - - public void updateFromLocalDatabase() { - int contactsAdded = 0; - int contactsRemoved = 0; - BatchOperationHandle batchOperation = directory.startBatchOperation(); - - try { - logger.info("Updating from local DB."); - int offset = 0; - - for (;;) { - List accounts = accountsManager.getAll(offset, CHUNK_SIZE); - - if (accounts == null || accounts.isEmpty()) break; - else offset += accounts.size(); - - for (Account account : accounts) { - if (account.isActive()) { - byte[] token = Util.getContactToken(account.getNumber()); - ClientContact clientContact = new ClientContact(token, null, true, true); - - directory.add(batchOperation, clientContact); - contactsAdded++; - } else { - directory.remove(batchOperation, account.getNumber()); - contactsRemoved++; - } - } - - logger.info("Processed " + CHUNK_SIZE + " local accounts..."); - } - } finally { - directory.stopBatchOperation(batchOperation); - } - - logger.info(String.format("Local directory is updated (%d added, %d removed).", contactsAdded, contactsRemoved)); - } - -// public void updateFromPeers() { -// logger.info("Updating peer directories."); -// -// int contactsAdded = 0; -// int contactsRemoved = 0; -// List clients = federatedClientManager.getClients(); -// -// for (FederatedClient client : clients) { -// logger.info("Updating directory from peer: " + client.getPeerName()); -// -// int userCount = client.getUserCount(); -// int retrieved = 0; -// -// logger.info("Remote peer user count: " + userCount); -// -// while (retrieved < userCount) { -// logger.info("Retrieving remote tokens..."); -// List remoteContacts = client.getUserTokens(retrieved); -// List localContacts = new LinkedList<>(); -// BatchOperationHandle handle = directory.startBatchOperation(); -// -// if (remoteContacts == null) { -// logger.info("Remote tokens empty, ending..."); -// break; -// } else { -// logger.info("Retrieved " + remoteContacts.size() + " remote tokens..."); -// } -// -// for (ClientContact remoteContact : remoteContacts) { -// localContacts.add(directory.get(handle, remoteContact.getToken())); -// } -// -// directory.stopBatchOperation(handle); -// -// handle = directory.startBatchOperation(); -// Iterator remoteContactIterator = remoteContacts.iterator(); -// Iterator localContactIterator = localContacts.iterator(); -// -// while (remoteContactIterator.hasNext() && localContactIterator.hasNext()) { -// try { -// ClientContact remoteContact = remoteContactIterator.next(); -// Optional localContact = localContactIterator.next().get(); -// -// remoteContact.setRelay(client.getPeerName()); -// -// if (!remoteContact.isInactive() && (!localContact.isPresent() || client.getPeerName().equals(localContact.get().getRelay()))) { -// contactsAdded++; -// directory.add(handle, remoteContact); -// } else { -// if (localContact.isPresent() && client.getPeerName().equals(localContact.get().getRelay())) { -// contactsRemoved++; -// directory.remove(handle, remoteContact.getToken()); -// } -// } -// } catch (IOException e) { -// logger.warn("JSON Serialization Failed: ", e); -// } -// } -// -// directory.stopBatchOperation(handle); -// -// retrieved += remoteContacts.size(); -// logger.info("Processed: " + retrieved + " remote tokens."); -// } -// -// logger.info("Update from peer complete."); -// } -// -// logger.info("Update from peer directories complete."); -// logger.info(String.format("Added %d and removed %d remove contacts.", contactsAdded, contactsRemoved)); -// } -}