Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
K
key_value-server
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
amir.yosef
key_value-server
Commits
1ff4ee00
Commit
1ff4ee00
authored
Aug 25, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update builder pattern
parent
da928f4f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
81 deletions
+54
-81
Main.java
src/Main.java
+11
-57
Server.java
src/server/Server.java
+16
-8
ServerBuilder.java
src/server/ServerBuilder.java
+27
-16
No files found.
src/Main.java
View file @
1ff4ee00
import
director.Director
;
import
server.ReplicaConnectionService
;
import
server.Server
;
import
server.ServerBuilder
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
public
class
Main
{
p
ublic
static
ExecutorService
executor
;
p
rivate
static
final
Logger
logger
=
Logger
.
getLogger
(
Main
.
class
.
getName
())
;
public
static
void
main
(
String
[]
args
)
{
int
availableProcessors
=
Runtime
.
getRuntime
().
availableProcessors
();
executor
=
Executors
.
newFixedThreadPool
(
availableProcessors
);
Director
director
=
new
Director
();
ServerBuilder
builder
=
new
ServerBuilder
(
args
);
List
<
Callable
<
Void
>>
tasks
=
Arrays
.
asList
(
()
->
{
try
{
ReplicaConnectionService
service
=
null
;
director
.
buildReplica
(
builder
,
service
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
null
;
},
()
->
{
try
(
Server
server
=
builder
.
build
())
{
server
.
start
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
null
;
}
);
try
{
executor
.
invokeAll
(
tasks
);
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
throw
new
RuntimeException
(
"Task execution was interrupted"
,
e
);
}
finally
{
shutdownExecutor
(
executor
);
}
}
private
static
void
shutdownExecutor
(
ExecutorService
executor
)
{
executor
.
shutdown
();
logger
.
info
(
"Starting application"
);
try
{
if
(!
executor
.
awaitTermination
(
60
,
TimeUnit
.
SECONDS
))
{
executor
.
shutdownNow
();
if
(!
executor
.
awaitTermination
(
60
,
TimeUnit
.
SECONDS
))
{
System
.
err
.
println
(
"Executor did not terminate"
);
}
ServerBuilder
builder
=
new
ServerBuilder
(
args
);
try
(
Server
server
=
builder
.
build
())
{
server
.
start
();
}
}
catch
(
InterruptedException
e
)
{
executor
.
shutdownNow
();
Thread
.
currentThread
().
interrupt
();
}
catch
(
Exception
e
)
{
logger
.
log
(
Level
.
SEVERE
,
"An error occurred"
,
e
);
}
logger
.
info
(
"Application shutdown complete"
);
}
}
}
\ No newline at end of file
src/server/Server.java
View file @
1ff4ee00
...
...
@@ -15,15 +15,26 @@ import java.util.logging.Logger;
public
class
Server
implements
AutoCloseable
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
Server
.
class
.
getName
());
private
final
int
PORT
;
private
final
String
role
;
private
final
ExecutorService
executor
;
private
final
StorageManager
manager
=
new
StorageManager
();
private
final
StorageManager
manager
;
private
final
ReplicaConnectionService
replicaConnectionService
;
public
Server
(
int
port
)
{
Server
(
int
port
,
String
role
,
ReplicaConnectionService
replicaConnectionService
)
{
logger
.
info
(
"Creating Server instance"
);
this
.
PORT
=
port
;
this
.
role
=
role
;
this
.
executor
=
Executors
.
newVirtualThreadPerTaskExecutor
();
this
.
manager
=
new
StorageManager
();
this
.
replicaConnectionService
=
replicaConnectionService
;
}
public
void
start
()
{
logger
.
info
(
"Starting server on port "
+
PORT
+
" with role "
+
role
);
if
(
"slave"
.
equals
(
role
)
&&
replicaConnectionService
!=
null
)
{
replicaConnectionService
.
checkConnection
();
}
try
(
ServerSocket
serverSocket
=
new
ServerSocket
(
PORT
))
{
serverSocket
.
setReuseAddress
(
true
);
logger
.
info
(
"Server started on Port "
+
PORT
);
...
...
@@ -61,12 +72,9 @@ public class Server implements AutoCloseable {
@Override
public
void
close
()
{
try
{
if
(!
executor
.
isShutdown
())
{
executor
.
shutdown
();
}
}
catch
(
Exception
e
)
{
logger
.
log
(
Level
.
SEVERE
,
"Failed to shutdown executor"
,
e
);
logger
.
info
(
"Shutting down server"
);
if
(!
executor
.
isShutdown
())
{
executor
.
shutdown
();
}
manager
.
shutdown
();
}
...
...
src/server/ServerBuilder.java
View file @
1ff4ee00
...
...
@@ -5,45 +5,56 @@ import util.Settings;
import
java.io.IOException
;
import
java.util.Map
;
import
java.util.
Optional
;
import
java.util.
logging.Logger
;
public
class
ServerBuilder
{
private
int
port
=
16379
;
private
String
role
;
private
final
String
[]
masterPortAndHost
;
private
final
static
ServerInfo
info
=
ServerInfo
.
getInstance
();
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ServerBuilder
.
class
.
getName
());
private
int
port
=
6379
;
private
String
role
=
"master"
;
private
String
[]
masterPortAndHost
;
private
final
ServerInfo
serverInfo
;
private
final
RdbFileInfo
rdbFileInfo
;
private
final
Map
<
String
,
String
>
parameters
;
private
static
final
RdbFileInfo
rdbfileInfo
=
RdbFileInfo
.
getInstance
()
;
private
ReplicaConnectionService
replicaConnectionService
;
public
ServerBuilder
(
String
[]
args
)
{
logger
.
info
(
"Initializing ServerBuilder"
);
this
.
parameters
=
Settings
.
extractArgs
(
args
);
this
.
port
=
Settings
.
extractPort
(
parameters
,
port
);
this
.
masterPortAndHost
=
initializeMasterPortAndHost
();
this
.
serverInfo
=
ServerInfo
.
getInstance
();
this
.
rdbFileInfo
=
RdbFileInfo
.
getInstance
();
initializeFromParameters
();
}
public
ServerBuilder
setPort
(
int
port
)
{
logger
.
info
(
"Setting port to "
+
port
);
this
.
port
=
port
;
return
this
;
}
public
void
setRole
(
String
role
)
{
public
ServerBuilder
setRole
(
String
role
)
{
logger
.
info
(
"Setting role to "
+
role
);
this
.
role
=
role
;
return
this
;
}
public
String
[]
getMasterPortAndHost
()
{
return
Optional
.
ofNullable
(
masterPortAndHost
).
orElseGet
(
this
::
initializeMasterPortAndHost
)
;
return
masterPortAndHost
;
}
public
Server
build
()
throws
IOException
{
if
(
role
==
null
)
{
role
=
"master"
;
logger
.
info
(
"Building Server instance"
);
if
(
"slave"
.
equals
(
role
)
&&
masterPortAndHost
!=
null
)
{
this
.
replicaConnectionService
=
new
ReplicaConnectionService
(
masterPortAndHost
,
port
);
}
return
new
Server
(
port
);
return
new
Server
(
port
,
role
,
replicaConnectionService
);
}
private
String
[]
initializeMasterPortAndHost
()
{
rdbfileInfo
.
setFile
(
parameters
);
return
info
.
findRole
(
parameters
);
private
void
initializeFromParameters
()
{
logger
.
info
(
"Initializing from parameters"
);
rdbFileInfo
.
setFile
(
parameters
);
this
.
masterPortAndHost
=
serverInfo
.
findRole
(
parameters
);
this
.
role
=
serverInfo
.
getRole
();
logger
.
info
(
"Initialized with role: "
+
role
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment