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
Show 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.Server
;
import
server.ServerBuilder
;
import
server.ServerBuilder
;
import
java.io.IOException
;
import
java.util.logging.Level
;
import
java.util.Arrays
;
import
java.util.logging.Logger
;
import
java.util.List
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
public
class
Main
{
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
)
{
public
static
void
main
(
String
[]
args
)
{
int
availableProcessors
=
Runtime
.
getRuntime
().
availableProcessors
();
logger
.
info
(
"Starting application"
);
executor
=
Executors
.
newFixedThreadPool
(
availableProcessors
);
Director
director
=
new
Director
();
ServerBuilder
builder
=
new
ServerBuilder
(
args
);
List
<
Callable
<
Void
>>
tasks
=
Arrays
.
asList
(
()
->
{
try
{
try
{
ReplicaConnectionService
service
=
null
;
ServerBuilder
builder
=
new
ServerBuilder
(
args
);
director
.
buildReplica
(
builder
,
service
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
null
;
},
()
->
{
try
(
Server
server
=
builder
.
build
())
{
try
(
Server
server
=
builder
.
build
())
{
server
.
start
();
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
();
try
{
if
(!
executor
.
awaitTermination
(
60
,
TimeUnit
.
SECONDS
))
{
executor
.
shutdownNow
();
if
(!
executor
.
awaitTermination
(
60
,
TimeUnit
.
SECONDS
))
{
System
.
err
.
println
(
"Executor did not terminate"
);
}
}
}
}
catch
(
InterruptedException
e
)
{
}
catch
(
Exception
e
)
{
executor
.
shutdownNow
();
logger
.
log
(
Level
.
SEVERE
,
"An error occurred"
,
e
);
Thread
.
currentThread
().
interrupt
();
}
}
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;
...
@@ -15,15 +15,26 @@ import java.util.logging.Logger;
public
class
Server
implements
AutoCloseable
{
public
class
Server
implements
AutoCloseable
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
Server
.
class
.
getName
());
private
static
final
Logger
logger
=
Logger
.
getLogger
(
Server
.
class
.
getName
());
private
final
int
PORT
;
private
final
int
PORT
;
private
final
String
role
;
private
final
ExecutorService
executor
;
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
.
PORT
=
port
;
this
.
role
=
role
;
this
.
executor
=
Executors
.
newVirtualThreadPerTaskExecutor
();
this
.
executor
=
Executors
.
newVirtualThreadPerTaskExecutor
();
this
.
manager
=
new
StorageManager
();
this
.
replicaConnectionService
=
replicaConnectionService
;
}
}
public
void
start
()
{
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
))
{
try
(
ServerSocket
serverSocket
=
new
ServerSocket
(
PORT
))
{
serverSocket
.
setReuseAddress
(
true
);
serverSocket
.
setReuseAddress
(
true
);
logger
.
info
(
"Server started on Port "
+
PORT
);
logger
.
info
(
"Server started on Port "
+
PORT
);
...
@@ -61,13 +72,10 @@ public class Server implements AutoCloseable {
...
@@ -61,13 +72,10 @@ public class Server implements AutoCloseable {
@Override
@Override
public
void
close
()
{
public
void
close
()
{
try
{
logger
.
info
(
"Shutting down server"
);
if
(!
executor
.
isShutdown
())
{
if
(!
executor
.
isShutdown
())
{
executor
.
shutdown
();
executor
.
shutdown
();
}
}
}
catch
(
Exception
e
)
{
logger
.
log
(
Level
.
SEVERE
,
"Failed to shutdown executor"
,
e
);
}
manager
.
shutdown
();
manager
.
shutdown
();
}
}
}
}
src/server/ServerBuilder.java
View file @
1ff4ee00
...
@@ -5,45 +5,56 @@ import util.Settings;
...
@@ -5,45 +5,56 @@ import util.Settings;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.
Optional
;
import
java.util.
logging.Logger
;
public
class
ServerBuilder
{
public
class
ServerBuilder
{
private
int
port
=
16379
;
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ServerBuilder
.
class
.
getName
());
private
String
role
;
private
int
port
=
6379
;
private
final
String
[]
masterPortAndHost
;
private
String
role
=
"master"
;
private
final
static
ServerInfo
info
=
ServerInfo
.
getInstance
();
private
String
[]
masterPortAndHost
;
private
final
ServerInfo
serverInfo
;
private
final
RdbFileInfo
rdbFileInfo
;
private
final
Map
<
String
,
String
>
parameters
;
private
final
Map
<
String
,
String
>
parameters
;
private
static
final
RdbFileInfo
rdbfileInfo
=
RdbFileInfo
.
getInstance
()
;
private
ReplicaConnectionService
replicaConnectionService
;
public
ServerBuilder
(
String
[]
args
)
{
public
ServerBuilder
(
String
[]
args
)
{
logger
.
info
(
"Initializing ServerBuilder"
);
this
.
parameters
=
Settings
.
extractArgs
(
args
);
this
.
parameters
=
Settings
.
extractArgs
(
args
);
this
.
port
=
Settings
.
extractPort
(
parameters
,
port
);
this
.
port
=
Settings
.
extractPort
(
parameters
,
port
);
this
.
masterPortAndHost
=
initializeMasterPortAndHost
();
this
.
serverInfo
=
ServerInfo
.
getInstance
();
this
.
rdbFileInfo
=
RdbFileInfo
.
getInstance
();
initializeFromParameters
();
}
}
public
ServerBuilder
setPort
(
int
port
)
{
public
ServerBuilder
setPort
(
int
port
)
{
logger
.
info
(
"Setting port to "
+
port
);
this
.
port
=
port
;
this
.
port
=
port
;
return
this
;
return
this
;
}
}
public
void
setRole
(
String
role
)
{
public
ServerBuilder
setRole
(
String
role
)
{
logger
.
info
(
"Setting role to "
+
role
);
this
.
role
=
role
;
this
.
role
=
role
;
return
this
;
}
}
public
String
[]
getMasterPortAndHost
()
{
public
String
[]
getMasterPortAndHost
()
{
return
Optional
.
ofNullable
(
masterPortAndHost
).
orElseGet
(
this
::
initializeMasterPortAndHost
)
;
return
masterPortAndHost
;
}
}
public
Server
build
()
throws
IOException
{
public
Server
build
()
throws
IOException
{
if
(
role
==
null
)
{
logger
.
info
(
"Building Server instance"
);
role
=
"master"
;
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
()
{
private
void
initializeFromParameters
()
{
rdbfileInfo
.
setFile
(
parameters
);
logger
.
info
(
"Initializing from parameters"
);
return
info
.
findRole
(
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