diff --git a/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/Keys.java b/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/Keys.java new file mode 100644 index 0000000000000000000000000000000000000000..96509383d5f8e564b9f480baa862bc2277a6b0c0 --- /dev/null +++ b/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/Keys.java @@ -0,0 +1,28 @@ +/** +* BigBlueButton open source conferencing system - http://www.bigbluebutton.org/ +* +* Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below). +* +* This program is free software; you can redistribute it and/or modify it under the +* terms of the GNU Lesser General Public License as published by the Free Software +* Foundation; either version 3.0 of the License, or (at your option) any later +* version. +* +* BigBlueButton 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 Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License along +* with BigBlueButton; if not, see <http://www.gnu.org/licenses/>. +* +*/ + +package org.bigbluebutton.common2.redis; + +public final class Keys { + public static final String MEETING = "meeting-"; + public static final String MEETINGS = "meetings"; + public static final String MEETING_INFO = "meeting:info:"; + public static final String BREAKOUT_MEETING = "meeting:breakout:"; + public static final String BREAKOUT_ROOMS = "meeting:breakout:rooms:"; +} diff --git a/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/RedisStorageService.java b/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/RedisStorageService.java index d4669ede5a8001cee7893ad0b7434652c43ef3cd..33133cf4528755e1de0540567d0aed74792d4329 100644 --- a/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/RedisStorageService.java +++ b/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/RedisStorageService.java @@ -1,7 +1,27 @@ +/** +* BigBlueButton open source conferencing system - http://www.bigbluebutton.org/ +* +* Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below). +* +* This program is free software; you can redistribute it and/or modify it under the +* terms of the GNU Lesser General Public License as published by the Free Software +* Foundation; either version 3.0 of the License, or (at your option) any later +* version. +* +* BigBlueButton 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 Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License along +* with BigBlueButton; if not, see <http://www.gnu.org/licenses/>. +* +*/ + package org.bigbluebutton.common2.redis; import java.util.Map; +import org.bigbluebutton.common2.redis.commands.MeetingCommands; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,6 +30,8 @@ import io.lettuce.core.RedisClient; import io.lettuce.core.RedisURI; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.api.sync.RedisCommands; +import io.lettuce.core.dynamic.RedisCommandFactory; +import io.lettuce.core.output.StatusOutput; public class RedisStorageService { @@ -23,10 +45,11 @@ public class RedisStorageService { private int port; private String clientName; + MeetingCommands meetingCommands; + public void start() { log.info("Starting RedisStorageService"); - RedisURI redisUri = RedisURI.Builder.redis(this.host, this.port) - .withClientName(this.clientName).build(); + RedisURI redisUri = RedisURI.Builder.redis(this.host, this.port).withClientName(this.clientName).build(); // @todo Add password if provided // if (!this.password.isEmpty()) { // redisUri.setPassword(this.password); @@ -36,6 +59,12 @@ public class RedisStorageService { redisClient.setOptions(ClientOptions.builder().autoReconnect(true).build()); connection = redisClient.connect(); + RedisCommandFactory factory = new RedisCommandFactory(connection); + initCommands(factory); + } + + private void initCommands(RedisCommandFactory factory) { + meetingCommands = factory.getCommands(MeetingCommands.class); } public void stop() { @@ -45,55 +74,32 @@ public class RedisStorageService { } public void recordMeetingInfo(String meetingId, Map<String, String> info) { - RedisCommands<String, String> commands = connection.sync(); - try { - if (log.isDebugEnabled()) { - for (Map.Entry<String, String> entry : info.entrySet()) { - log.debug("Storing metadata {} = {}", entry.getKey(), entry.getValue()); - } - } - - log.debug("Saving metadata in {}", meetingId); - commands.hmset("meeting:info:" + meetingId, info); - } catch (Exception e) { - log.warn("Cannot record the info meeting: {}", meetingId, e); - } finally { - connection.close(); - } + log.debug("Storing meeting {} metadata {}", meetingId, info); + recordMeeting(Keys.MEETING_INFO + meetingId, info); } public void recordBreakoutInfo(String meetingId, Map<String, String> breakoutInfo) { - RedisCommands<String, String> commands = connection.sync(); - try { - log.debug("Saving breakout metadata in {}", meetingId); - commands.hmset("meeting:breakout:" + meetingId, breakoutInfo); - } catch (Exception e) { - log.warn("Cannot record the info meeting: {}", meetingId, e); - } finally { - connection.close(); - } + log.debug("Saving breakout metadata in {}", meetingId); + recordMeeting(Keys.BREAKOUT_MEETING + meetingId, breakoutInfo); } public void addBreakoutRoom(String parentId, String breakoutId) { - RedisCommands<String, String> commands = connection.sync(); - try { - log.debug("Saving breakout room for meeting {}", parentId); - commands.sadd("meeting:breakout:rooms:" + parentId, breakoutId); - } catch (Exception e) { - log.warn("Cannot record the info meeting:" + parentId, e); - } finally { - connection.close(); - } + log.debug("Saving breakout room for meeting {}", parentId); + meetingCommands.addBreakoutRooms(Keys.BREAKOUT_ROOMS + parentId, breakoutId); } + // @fixme: not used anywhere public void removeMeeting(String meetingId) { + log.debug("Removing meeting meeting {} inside a transaction", meetingId); RedisCommands<String, String> commands = connection.sync(); - try { - commands.del("meeting-" + meetingId); - commands.srem("meetings", meetingId); - } finally { - connection.close(); - } + commands.multi(); + meetingCommands.deleteMeeting(Keys.MEETING + meetingId); + meetingCommands.deleteMeetings(Keys.MEETINGS + meetingId); + commands.exec(); + } + + private String recordMeeting(String key, Map<String, String> info) { + return meetingCommands.recordMeetingInfo(key, info); } public void setPassword(String password) { diff --git a/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/commands/MeetingCommands.java b/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/commands/MeetingCommands.java new file mode 100644 index 0000000000000000000000000000000000000000..12b8a5312f19294a32adb842b388ff4c27729cc3 --- /dev/null +++ b/bbb-common-message/src/main/java/org/bigbluebutton/common2/redis/commands/MeetingCommands.java @@ -0,0 +1,42 @@ +/** +* BigBlueButton open source conferencing system - http://www.bigbluebutton.org/ +* +* Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below). +* +* This program is free software; you can redistribute it and/or modify it under the +* terms of the GNU Lesser General Public License as published by the Free Software +* Foundation; either version 3.0 of the License, or (at your option) any later +* version. +* +* BigBlueButton 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 Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License along +* with BigBlueButton; if not, see <http://www.gnu.org/licenses/>. +* +*/ + +package org.bigbluebutton.common2.redis.commands; + +import java.util.List; +import java.util.Map; + +import io.lettuce.core.dynamic.Commands; +import io.lettuce.core.dynamic.annotation.Command; + +public interface MeetingCommands extends Commands { + List<String> mget(String... keys); + + @Command("DEL ?0") + String deleteMeeting(String meetingKey); + + @Command("SREM ?0") + String deleteMeetings(String meetingsKey); + + @Command("HMSET ?0 ?1") + String recordMeetingInfo(String meetingKey, Map<String, String> values); + + @Command("SADD ?0 ?1") + String addBreakoutRooms(String parentKey, String breakoutId); +}