%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Short Sectioned Assignment
% LaTeX Template
% Version 1.0 (5/5/12)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% Original author:
% Frits Wenneker (http://www.howtotex.com)
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%   PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[paper=letter, fontsize=11pt]{scrartcl} % A4 paper and 11pt font size

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage{fourier} % Use the Adobe Utopia font for the document - comment this line to return to the LaTeX default
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm} % Math packages
\usepackage{graphicx}
\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template
\usepackage{hyperref}
\usepackage{amssymb}
\usepackage{listings}
\usepackage[]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{enumerate}
\usepackage[table,xcdraw]{xcolor}
\usepackage{sectsty} % Allows customizing section commands
\usepackage{float}
\usepackage{caption}
\usepackage{gensymb} % to used degree symbol 
\usepackage{siunitx} 
\usepackage{enumitem}

\usepackage[sc]{mathpazo}
\allsectionsfont{ \normalfont\scshape} % Make all sections the default font and small caps
\usepackage{fancyhdr} % Custom headers and footers
\pagestyle{fancyplain} % Makes all pages in the document conform to the custom headers and footers
\fancyhead{} % No page header - if you want one, create it in the same way as the footers below
\fancyfoot[L]{} % Empty left footer
\fancyfoot[C]{} % Empty center footer
\fancyfoot[R]{\thepage} % Page numbering for right footer
\renewcommand{\headrulewidth}{0pt} % Remove header underlines
\renewcommand{\footrulewidth}{0pt} % Remove footer underlines
\setlength{\headheight}{13.6pt} % Customize the height of the header

\numberwithin{equation}{section} % Number equations within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)
\numberwithin{figure}{section} % Number figures within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)
\numberwithin{table}{section} % Number tables within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)

\setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text

%----------------------------------------------------------------------------------------
%   TITLE SECTION
%----------------------------------------------------------------------------------------
\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Create horizontal rule command with 1 argument of height

\title{ 
\normalfont \normalsize 
\textsc{University of California Irvine} \\  % Your university, school and/or department name(s)
\textsc{Prgramming Language Research Group} \\ [25pt]
\horrule{0.5pt} \\[0.4cm] % Thin top horizontal rule
\huge IoTCloud Version 2.0\\ % The assignment title
\horrule{2pt} \\[0.5cm] % Thick bottom horizontal rule
}

\author{Authors} % Your name


\date{\normalsize\today} % Today's date or a custom date

\begin{document}

\maketitle % Print the title




%---------------------------------------------------------------------------------------
% Custom Stuff
%---------------------------------------------------------------------------------------
\newcommand{\tab}[1]{\hspace{.2\textwidth}\rlap{#1}}




\section{\textbf{Introduction}}

\section{\textbf{Approach}}

\subsection{\textbf{Records}}
Each record has the following information included in it:
\begin{itemize}
    \item Machine ID of the device creating the record
    \item The vector clock using the largest clock values from each device it knows and its own largest clock value incremented by 1.
    \item Add a random salt (or nonce) for the encryption safety
    \item Data payload
    \item HMAC of the record.
\end{itemize}
    


\subsubsection{\textbf{Types of Payloads}}
The different types of record payloads are:
\begin{itemize}
    \item Delete notifications
        \begin{itemize}
            \item Contains the HMAC of records that were deleted by devices, Their vector clocks and the server sequence numbers.
            \item Generated when a device deletes a record.
            \item The delete notification with the largest server sequence number in its delete payload is the live one (the one that contains the largest server sequence number of the record deleted to date).  
        \end{itemize}
    \item Commit notifications
        \begin{itemize}
            \item Contains list of transactions that are committed in order of commit and the current key-value pair for that key.
            \item Generated by the arbitrator of a key and only the for that key (1 arbitrator per key).
            \item Used in the case that there is a key value pair that needs reordering.
        \end{itemize}
    \item Abort notifications
        \begin{itemize}
            \item Contains a transaction ID of an aborted transaction and the machine ID of the device that created that transaction.
            \item Causes a transaction to be aborted.
        \end{itemize}
    \item Abort acknowledgement notifications
        \begin{itemize}
            \item Contains a transaction ID of an aborted transaction, the machine ID of the device that created that transaction and the abort notification ID that this is acknowledging.
            \item Causes an abort notification to become dead.
            \item Is generated by the device that had created an aborted transaction as an acknowledgement that it saw the aborted transaction notification.
            \item This payload type immediately becomes dead (not live) upon insertion into the data structure.
        \end{itemize}
    \item Data structure re-size notifications
        \begin{itemize}
            \item Contains new size of data structure (number of record allowed in the data structure or something like that).
            \item Causes old data Structure re-size notification to no longer be live.
        \end{itemize}
    \item Server sequence number for a specific record notifications
        \begin{itemize}
            \item Contains a record HMAC and the server sequence number for that record
        \end{itemize}
    \item Transactions
        \begin{itemize}
            \item Contains:
            \begin{itemize}
                \item Transaction ID
                \item A guard condition that can be evaluated
                \item A set of key-value pairs that are to be updated if the guard condition is met.
            \end{itemize}
        \end{itemize}
\end{itemize}


\subsection{\textbf{Updates (Online Updates)}}
Updates take place as follows:
\begin{enumerate}
    \item A device pulls the latest version of the data structure.  If the device cannot pull the latest version because of network connectivity or some other issues then that device will just work using the local copy of the data structure it has.
    \item The device makes a record as follows:
        \begin{enumerate}
            \item Adds its machine ID.
            \item Creates a vector clock using the largest clock values from each device it knows and its own largest clock value incremented by 1.
            \item Add a random salt (or nonce) for the encryption safety
            \item Fill the record data section with the transactions, key-value pairs, ext.
            \item Fill the remainder of the data section with rescued key-value pairs, transactions, ext (Discussed later).
            \item Pad the record to be the same size for all records.
            \item Calculate the HMAC of the record and add that to the record
            \item Encrypt the record.
        \end{enumerate}
    \item Send the record to the server for insertion into the device's queue.
    \item Wait for response from server stating the new records (the one just sent) server sequence number.  Save this server sequence number for when creating the next record.
    \item 
\end{enumerate}


\subsection{\textbf{Updates when offline}}
When offline and making updates, the devices should use their local copy of the data structure but do no deletes.  When connection is reestablished the following should take place:

\begin{enumerate}
    \item Pull the latest version of the data structure.
    \item Update local copy of the data structure except for own devices device queue (do deletions as needed)
    \item Calculate many records are "new" to the data structure and pick the same amount to be deleted
    \item Push the updates and the deletes to the server
    \item Wait for sequence numbers for the recently pushed records
    \item Push the sequence numbers for the recently pushed records (using online updates from the section above)
\end{enumerate}

This kind of update will result in the latest key-value pair being the last pushed record from this update (if no other updates are occurring at the same time).  The arbitrator can then commit or abort as needed but in the mean time the key-value pair may be an old one (but have the largest server sequence number).

\subsection{\textbf{Deletions}}
When deciding which records to delete the following is to be done:
\begin{enumerate}
    \item Order all the records in order based on their server sequence numbers
    \item Calculate the difference between the current size of the data structure and the minimum size of the data structure (lets call this $m$)
    \item Delete the oldest m records based on the ordering from step 1. 
    \begin{itemize}
        \item If a record to be deleted has live data in it then the whole data structure needs to be resized.
    \end{itemize}
\end{enumerate}

Note this makes that size of the data structure be bounded.
If there are $n$ devices and the data structure has a minimum size of $m$.  Then the max size of the data structure is given by $m + n$ for the case when all the devices make an update at the same time.   

\subsection{\textbf{Rescuing Transactions, Commits, Aborts, Ext}}
Data should be proactively rescued from the "oldest" records currently in the data structure.  Unused space in new records should be used to rescue data from old records so that when it comes time to delete the old records, there are no live pieces of data that need to be rescued.  When a piece of data is rescued, it is rescued with its vector clock as well (so that the time of that data can be saved).

When deciding which data to rescue the following is to be done:
\begin{enumerate}
    \item Order all the records in order based on their server sequence numbers
    \item Create an ordered list of currently live transactions, commits, aborts, ext from the oldest $n$ records from step one where the order is based on the age of the data (how old the record .
    \item Randomly select from the list of live data to save.  Save as much as can fit in the current new record.  The random selection could give higher probability to data from records that are to be deleted sooner.
\end{enumerate}

If a record needs to be deleted but still contains live data then the data structure needs to be resized.  

\subsection{\textbf{Checking the Data Structure}}
Checking the data structure for consistency is done as follows:
\begin{enumerate}
    \item Verify that each record in the data structure has an HMAC that matches the data in the record.
    \item Verify that there are at least as many records in the data structure as stated in the largest data structure size record.
    \item Make sure that for each device queue the difference between the vector clock value of the device queues clock is at most 1 between 2 consecutive messages.
    \item Verify that no currently live data Structure re-size notification is smaller than the last known data structure size.  Data structure can only grow in size.
    \item Verify that all the server sequence numbers for the records that are currently present have unique numbers that have a difference of 1 (no gaps).
    \item Verify record server sequence numbers against the stated server sequence numbers in the server sequence number notification payloads (make sure the server is not changing the sequence number on the fly).
    \item 
\end{enumerate}


\subsection{\textbf{The Arbitrator}}
The arbitrator can:
\begin{enumerate}
    \item Send Commits
    \item Send Aborts
\end{enumerate}
    
\subsubsection{\textbf{Commits}}
Commits have the following properties
\begin{itemize}
    \item Agree with the ordering of the server sequence numbers
    \item Once a key-value pair is commited it can not be commited again.
    \item Cannot commit an already aborted transaction.
    \item Commits state the ordering of key-value pairs.
    \item Can disagree with the ordering of server sequence numbers but
    \item Should occur frequently as to make sure that the commit order matches the server sequence ordering as closely as possible (prevent large divergence of the 2 orderings)
\end{itemize}
    
\subsubsection{\textbf{Aborts}}
Aborts are used to show which transactions have been aborted and will not be used in the total ordering of the transactions.  Aborts are considered live until an abort acknowledgement is presented.
When the transaction is aborted then the devices should simply act as if it were never present when evaluating for the latest key-value pair.
    
\subsection{\textbf{Live Status}}
Live Status of entries:
\begin{enumerate}
    \item Key-Value Entry/Data Entry is dead if either:
    \begin{enumerate}
        \item There is a newer key-value pair:
        \begin{itemize}
            \item There is a transaction with a newer vector clock value.
            \item There is a commit that for this key-value pair.
            \item There is an abort for this key-value pair.
        \end{itemize}
        \item It is incomplete.
        \item It is an abort notification that has an abort notification acknowledgment
        \item It is an abort notification acknowledgment (dead on arrival).
    \end{enumerate}
    \item Data is live if there are multiple versions of the same data (key-value pair) in which the vector clock values show concurrency.  All versions are kept live the arbitrator arbitrates.
    \item Multiple versions of the same data (same transaction ID for example) are not all live.  Only the version with the largest server sequence number is live.  
    
\end{enumerate}

    

\end{document}